【问题标题】:Can not resolve reference: `Windows.Foundation.UniversalApiContract` in Android Application无法解析参考:Android 应用程序中的“Windows.Foundation.UniversalApiContract”
【发布时间】:2020-02-08 22:45:43
【问题描述】:

在我的用于 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# android xamarin.forms


    【解决方案1】:

    好的,如果您知道该怎么做,这很容易。引用不应位于主 .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 标准库中删除了引用,错误消失了。

    【讨论】:

    • 已确认。也适用于 Android。
    猜你喜欢
    • 2020-12-27
    • 1970-01-01
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    • 1970-01-01
    相关资源
    最近更新 更多