c# - 无法解析参考: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 应用程序中找到本地存储的正确路径。我该如何解决这个问题?
解决方案
好的,如果您知道该怎么做,这很容易。引用不应位于主 .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 标准库中删除了引用,错误消失了。
推荐阅读
- reactjs - React-Redux useSelector 在测试中的奇怪行为
- postgresql - 为什么我可以直接在我的批处理文件中调用 pg_dump 而不是 pg_start_backup?
- c# - SQL 请求 - 显示两个日期之间条目的日期
- c - 如何将值存储到多参数结构中并将 typedef 结构传递给函数?
- javascript - 如何从不同的数据列表中选择值并一次显示在同一个文本区域框中?
- powershell - 如何检查 PowerShell 变量是否是有序哈希表?
- javascript - ngx-monaco-editor:无法从 node_modules 找到 loader.js 文件
- wordpress - 在wordpress PHP中将longdomain URL重写为短域URL
- c# - 使用“AutoMapper”将一个包含类列表的类映射到另一个类似属性的类中
- excel-formula - 自定义舍入规则