无法解析参考:Android 应用程序中的`Windows.Foundation.UniversalApiContract` [英] Can not resolve reference: `Windows.Foundation.UniversalApiContract` in Android Application

查看:15
本文介绍了无法解析参考:Android 应用程序中的`Windows.Foundation.UniversalApiContract`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在适用于 UWP、Android 和 iOS 的 Xamarin.Forms 应用程序(暂时卸载)中,我添加了以下代码:

In my Xamarin.Forms Application for UWP, Android and iOS (unloaded for the moment) I added the following code:

  StorageFolder localFolder = ApplicationData.Current.LocalFolder;

  databasePath = Path.Combine(localFolder.Path, databaseName);

这导致在 .Net Standard 库中添加了一个引用,如下所示.

This resulted in the addition of a reference in the .Net Standard library as follows.

<Reference Include="Windows.Foundation.UniversalApiContract">
    <HintPath>..\..\..\..\..\Program Files (x86)\Windows Kits\10\References\10.0.16299.0\Windows.Foundation.UniversalApiContract\5.0.0.0\Windows.Foundation.UniversalApiContract.winmd</HintPath>
  </Reference>

这反过来导致 Android 应用程序出现错误,文本如下:

This in turn resulted in an error on the Android Application with the following text:

Can not resolve reference: `Windows.Foundation.UniversalApiContract`, referenced by `eTabber`. Please add a NuGet package or assembly reference for `Windows.Foundation.UniversalApiContract`, or remove the reference to `eTabber`.

我确实需要对 Windows.Storage 的引用,以便能够在 UWP 应用程序中找到正确的本地存储路径.我该如何解决这个问题?

I do need the reference to Windows.Storage in order to be able to find the right path to the local storage in the UWP application. How do I solve this problem?

推荐答案

好吧,如果你知道该怎么做,这很容易.引用不应位于主 .Net Standard 库中,而应位于 UWP 应用程序中.所以我做了以下事情.

Ok, it's easy if you know what to do. The reference should not be in the main .Net Standard library but in the UWP Application. So I did the following.

在主 .Net Standard 库的 DeviceService 中,我创建了以下代码:

In the DeviceService in the main .Net Standard library I created the following code:

public class DeviceService
{
    private const string databaseName = "eTabber.db";

    public static string StoragePath { get; set; }

    /// <summary>
    /// Return the device specific database path for SQLite
    /// </summary>
    public static string GetSQLiteDatabasePath()
    {
        return Path.Combine(StoragePath, databaseName);
    }
}

然后在每个应用程序(UWP、iOS 或 Android)开始时填充静态属性 StoragePath.

The static property StoragePath is then filled at the start of each Application (UWP, iOS or Android).

App.Xaml.cs 中的 UWP:

UWP in App.Xaml.cs:

    public App()
    {
        this.InitializeComponent();
        this.Suspending += OnSuspending;

        DeviceService.StoragePath = StoragePath;
    }

    private string StoragePath => ApplicationData.Current.LocalFolder.Path;

MainActivity.cs 中的 Android(尚未对此进行测试):

Android in MainActivity.cs (haven't tested this yet):

    protected override void OnCreate(Bundle savedInstanceState)
    {
        DeviceService.StoragePath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        ...

iOS 中的 Main.cs(尚未对此进行测试):

iOS in Main.cs (haven't tested this yet):

    static void Main(string[] args)
    {
        DeviceService.StoragePath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);

至少对于 UWP 来说,这个构造工作正常.从 .Net Standard 库中删除了引用,错误消失了.

For at least UWP this construct works fine. Removed the reference from the .Net Standard library and the error disappeared.

这篇关于无法解析参考:Android 应用程序中的`Windows.Foundation.UniversalApiContract`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆