Xamarin Android - 无法找到类型或命名空间“Context”

时间:2017-11-21 04:58:11

标签: c# android visual-studio xamarin xamarin.android

我正在关注this Xamarin教程。当我在Android项目中使用“Context”类时,Visual Studio会引发错误。默认情况下,这个类不应该包含在我的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?)

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

便携式\ 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);
    }
}

1 个答案:

答案 0 :(得分:1)

试试这个:

using Android.Content;