简体   繁体   English

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

[英]Xamarin Android - Type or namespace “Context” could not be found

I'm following this Xamarin tutorial. 我正在关注这个 Xamarin教程。 Visual Studio throws an error when I use the "Context" class in my Android project. 当我在Android项目中使用“上下文”类时,Visual Studio会引发错误。 Shouldn't this class be included in my Android app by default? 默认情况下,此类不应该包含在我的Android应用程序中吗?

Also, I defined an interface named "IStreamLoader" in a Portable Class Library called "Portable", and added a reference to "Portable" in my Android project. 另外,我在可移植类库“ Portable”中定义了一个名为“ IStreamLoader”的接口,并在我的Android项目中添加了对“ Portable”的引用。 But referencing "IStreamLoader" in my Android project throws another error. 但是在我的Android项目中引用“ IStreamLoader”会引发另一个错误。

Are these two errors related? 这两个错误相关吗?

Errors 错误

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM