Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Xamarin Android-类型或名称空间;上下文“;找不到_C#_Android_Visual Studio_Xamarin_Xamarin.android - Fatal编程技术网

C# Xamarin Android-类型或名称空间;上下文“;找不到

C# Xamarin Android-类型或名称空间;上下文“;找不到,c#,android,visual-studio,xamarin,xamarin.android,C#,Android,Visual Studio,Xamarin,Xamarin.android,我正在学习Xamarin教程。当我在Android项目中使用“Context”类时,VisualStudio抛出一个错误。默认情况下,这个类不应该包含在我的Android应用程序中吗 此外,我在一个名为“Portable”的可移植类库中定义了一个名为“IStreamLoader”的接口,并在我的Android项目中添加了对“Portable”的引用。但在我的Android项目中引用“IStreamLoader”会引发另一个错误 这两个错误相关吗 错误 CS0246 The type or na

我正在学习Xamarin教程。当我在Android项目中使用“Context”类时,VisualStudio抛出一个错误。默认情况下,这个类不应该包含在我的Android应用程序中吗

此外,我在一个名为“Portable”的可移植类库中定义了一个名为“IStreamLoader”的接口,并在我的Android项目中添加了对“Portable”的引用。但在我的Android项目中引用“IStreamLoader”会引发另一个错误

这两个错误相关吗

错误

CS0246  The type or namespace name 'IStreamLoader' could not be found (are you missing a using directive or an assembly reference?)

CS0246  The type or namespace name 'Context' could not be found (are you missing a using directive or an assembly reference?)

CS0246  The type or namespace name 'Context' could not be found (are you missing a using directive or an assembly reference?)
using System.Linq;
using Android.App;
using Android.OS;

namespace MyTunes
{
    [Activity(Label = "My Tunes", MainLauncher = true)]
    public class MainActivity : ListActivity
    {
        protected async override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            var data = await SongLoader.Load();

            ListAdapter = new ListAdapter<Song>() {
                DataSource = data.ToList(),
                TextProc = s => s.Name,
                DetailTextProc = s => s.Artist + " - " + s.Album
            };
        }
    }

    public class StreamLoader : IStreamLoader
    {
        private readonly Context context;

        public StreamLoader(Context context)
        {
            this.context = context;
        }
        public Stream GetStreamForFilename(string filename)
        {
            return context.Assets.Open(filename);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Portable
{
    public interface IStreamLoader
    {
        System.IO.Stream GetStreamForFilename(string filename);
    }
}
MyTunes.Droid\MainActivity.cs

CS0246  The type or namespace name 'IStreamLoader' could not be found (are you missing a using directive or an assembly reference?)

CS0246  The type or namespace name 'Context' could not be found (are you missing a using directive or an assembly reference?)

CS0246  The type or namespace name 'Context' could not be found (are you missing a using directive or an assembly reference?)
using System.Linq;
using Android.App;
using Android.OS;

namespace MyTunes
{
    [Activity(Label = "My Tunes", MainLauncher = true)]
    public class MainActivity : ListActivity
    {
        protected async override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            var data = await SongLoader.Load();

            ListAdapter = new ListAdapter<Song>() {
                DataSource = data.ToList(),
                TextProc = s => s.Name,
                DetailTextProc = s => s.Artist + " - " + s.Album
            };
        }
    }

    public class StreamLoader : IStreamLoader
    {
        private readonly Context context;

        public StreamLoader(Context context)
        {
            this.context = context;
        }
        public Stream GetStreamForFilename(string filename)
        {
            return context.Assets.Open(filename);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Portable
{
    public interface IStreamLoader
    {
        System.IO.Stream GetStreamForFilename(string filename);
    }
}
试试这个:

using Android.Content;

哇,是的,修复了“上下文”错误,谢谢!您知道我的“IStreamLoader”引用可能失败的原因吗?请尝试使用Portable或公共类StreamLoader:Portable.IStreamLoader呃,显然Portable\IStreamLoader.cs命名空间必须设置为解决方案名称,而不是项目名称。尽管您的上述解决方案也有效。