c# - Shared Xamarin.Forms - 命名空间中不存在类型或命名空间名称 'App'

标签 c# android xamarin xamarin.forms xamarin.android

我正在通过 this Xamarin.Forms 教程。我按照说明构建了一个 Shared 项目,但我在下载一个文件时遇到错误,该文件应该让我的应用程序访问 Android 上的拨号功能。

这是我得到的完整错误:

CS0234  The type or namespace name 'App' does not exist in the namespace 'Phoneword.Android' (are you missing an assembly reference?)   Phoneword.Android

我在网上发现了一些类似的错误,包括 this one ,但没有一个解决方案对我有用。总体趋势似乎是 App 类存在于 Shared 项目中,因此损坏的引用可能会导致问题,但我删除了对 Shared 项目的 Android 引用,并且出现了一堆其他错误,然后我重新- 添加了它,除了“应用程序”之外的所有错误都消失了。所以我不认为这是一个引用问题。

PhoneDialer.Droid.cs(我标记了错误所在的行)

using Android.Content;
using System.Linq;
using System.Threading.Tasks;
using Android.Telephony;
using Xamarin.Forms;
using Phoneword.Android;

using Uri = Android.Net.Uri;
[assembly: Dependency(typeof(PhoneDialer))]

namespace Phoneword.Android
{
    public class PhoneDialer : IDialer
    {
        public Task<bool> DialAsync(string number)
        {
            var context = Android.App.Application.Context; //<=ERROR
            ...
        }
        ...
    }
}

当我将行更改为 Phoneword.App.Application.Context 时,“App”没有抛出任何错误,但随后我收到另一个错误,指出“Application”不存在。

有人知道问题出在哪里吗?

我将解决方案上传到 github,所以我的所有代码都可见:https://github.com/joeymorano/Phoneword

最佳答案

厚颜无耻的错误....

将您当前的命名空间 namespace Phoneword.Android 更改为 namespace Phoneword.Droid,一切正常

当您有名为 namespace Phoneword.Android 的命名空间并尝试访问 Android.App.Application.Context 时,Android. 称为您的当前项目...不是android原生库

enter image description here

关于c# - Shared Xamarin.Forms - 命名空间中不存在类型或命名空间名称 'App',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47502756/

相关文章:

c# - Html Agility Pack SelectSingleNode 在迭代中总是给出相同的结果?

C# WPF总是无法连接mysql数据库

android - google_play_service_lib.jar 丢失

android - 在 Android-x86 中使用 C2DM

android - 应用程序关闭时线程是否会停止?

c# - 为 Visual Studio 安装 Hyper-V 模拟器时出错

xamarin.ios - 从命令行获取 Xamarin 工具链版本信息

c# - ASP.NET核心: Custom IFileProvider prevents default IFileProvider from working

c# - 自定义 xsd.exe 工具和区分大小写的 xml 序列化

ios - 录制视频/音频时播放系统声音