Xamarin Android - 找不到类型或命名空间"Context"



我正在遵循这个Xamarin教程。Visual Studio在Android项目中使用"Context"类时抛出错误。默认情况下,这个类不应该包含在我的安卓应用程序中吗?

此外,我在一个名为"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?)

MyTunes.Droid\MainActivity.cs

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);
        }
    }
}

Portable\IStreamLoader.cs

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;

最新更新