首页 > 解决方案 > 无法解析参考:Android 应用程序中的`Windows.Foundation.UniversalApiContract`

问题描述

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

  StorageFolder localFolder = ApplicationData.Current.LocalFolder;

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

这导致在 .Net 标准库中添加了如下引用。

<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 应用程序出现错误,并显示以下文本:

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 应用程序中找到本地存储的正确路径。我该如何解决这个问题?

标签: c#androidxamarin.forms

解决方案


好的,如果您知道该怎么做,这很容易。引用不应位于主 .Net 标准库中,而应位于 UWP 应用程序中。所以我做了以下。

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

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。

App.Xaml.cs 中的 UWP:

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

        DeviceService.StoragePath = StoragePath;
    }

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

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

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

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

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

至少对于 UWP,此构造可以正常工作。从 .Net 标准库中删除了引用,错误消失了。


推荐阅读