簡體   English   中英

Xamarin Android-找不到類型或名稱空間“上下文”

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

我正在關注這個 Xamarin教程。 當我在Android項目中使用“上下文”類時,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);
    }
}

嘗試這個 :

using Android.Content;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM