【发布时间】:2018-02-13 00:14:32
【问题描述】:
我需要使用 SQlite 数据库“播种”我的应用程序。我使用了@Kasper 在帖子How to deploy a database file with a Xamarin.form app? 中建议的代码。
我的 UWP 代码如下所示。
public async Task<String> GetDBPathAndCreateIfNotExists()
{
String filename = "birdsnbflys.db3";
bool isExisting = false;
try
{
StorageFile storage = await ApplicationData.Current.LocalFolder.GetFileAsync(filename);
isExisting = true;
}
catch (Exception)
{
isExisting = false;
}
if (!isExisting)
{
StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync(filename);
await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder, filename, NameCollisionOption.ReplaceExisting);
}
return Path.Combine(ApplicationData.Current.LocalFolder.Path, filename);
}
当我在“本地机器”上运行它时,它“挂起/永不返回”
StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync(filename);
如果我查看 Package.Current.InstalledLocation 文件夹,数据库文件就在那里。
当我在其中一个设备模拟器上运行它时,它“挂起/永不返回”
await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder, filename, NameCollisionOption.ReplaceExisting);
在这种情况下,如果我退出应用程序然后重新启动它,文件现在位于 ApplicationData.Current.LocalFolder 中,因此应用程序运行。
鉴于我的应用程序的需要,我很乐意使用同步方式来执行此操作,但看起来微软除了异步之外没有提供任何东西。
关于如何按预期工作的任何想法或建议?
谢谢, 戴夫
【问题讨论】:
-
Async 是您在 UWP 应用中执行此操作的方式。不要期望发布同步 API。
标签: c# sqlite uwp xamarin.forms