tmi*_*hty 0 c# asynchronous xamarin storagefile visual-studio-2017
我需要从资源中的文本文件中读取数值(版本号).我将此版本号与已安装组件的版本号进行比较.如果资源中的版本号高于安装的版本,我将新组件(数据库)从我的资源复制到用户可以使用它的本地目录.
我需要同步执行此操作,因为没有数据库我的应用程序无法工作.
但是,我没有看到任何同步的方法.MS强迫我用这样的异步任务来做:
private async Task<string> ResourcesReadTextFile(string uFileName)
{
string sRet = "";
try
{
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(cstAssets + uFileName));
using (var inputStream = await file.OpenReadAsync())
using (var classicStream = inputStream.AsStreamForRead())
using (var streamReader = new StreamReader(classicStream))
{
while (streamReader.Peek() >= 0)
{
sRet = streamReader.ReadLine();
}
}
}
catch (Exception ex)
{
Debug.Assert(false);//check here
}
return sRet;
}
Run Code Online (Sandbox Code Playgroud)
现在我遇到了应用程序在数据库被复制到本地目录之前启动的情况,因为复制也需要异步完成,根本没有任何方法可以同步.没有StorageFile.Copy()这样的功能.
我之所以使用的是:
private async void pCopyFromResourcesToLocal(string uFileName)
{
// Cant await inside catch, but this works anyway
try
{
StorageFile storfile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(cstAssets + uFileName));
await storfile.CopyAsync(ApplicationData.Current.LocalFolder);
}
catch (Exception ex)
{
Debug.WriteLine("");
}
}
Run Code Online (Sandbox Code Playgroud)
这让我发疯了.
人们写道Async应该被拥抱和拥抱并欣赏,但在我的情况下它只会导致麻烦.
我没有看到任何让这个东西同步的方法,我想知道为什么MS强迫我这样做.
任何帮助非常感谢.
谢谢.
代码截图:
编辑:我在这里添加了顶级方法:
public static async Task<DB> InitAppDb()
{
IFileHelper helper = DependencyService.Get<IFileHelper>();
string path = await helper.GetFilePathAndCopyFromResourcesIfNotPresent("tablet.db");
return (_dbApp = new DB(path));
}
public async Task CopyDatabaseIfNotExists(string uFileName)
{
IsolatedStorageFile nExpectedFolder = IsolatedStorageFile.GetUserStoreForApplication();
bool bCopyNewDB = false;
Task<bool> datatask = pResourceIsNewer(uFileName);
bCopyNewDB = await datatask;
if (! bCopyNewDB)
{
try
{
await ApplicationData.Current.LocalFolder.GetFileAsync(uFileName); //nExpectedFolder.GetFileAsync(dbPath);/// ApplicationData.Current.LocalFolder.GetFileAsync("preinstalledDB.db");
// No exception means it exists
return;
}
catch (System.IO.FileNotFoundException)
{
// The file obviously doesn't exist
}
}
pCopyFromResourcesToLocal(uFileName);
}
private async Task<bool>pResourceIsNewer(string uPath)
{
string sFileNameAppDBVersion =uPath + ".txt";
if (IsolatedStorageFileExist(sFileNameAppDBVersion))
{
int iAppDBVersionInstalled = Convert.ToInt32(IsolatedStorageReadTextFile(sFileNameAppDBVersion));
Task<string> datatask = ResourcesReadTextFile(sFileNameAppDBVersion);
string s = await datatask;
int iAppDBResources = Convert.ToInt32(s);
bool b = (iAppDBResources > iAppDBVersionInstalled);
return b;
}
else
{
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
你所要做的就是:
//private async void pCopyFromResourcesToLocal(string uFileName) { ... }
private async Task pCopyFromResourcesToLocal(string uFileName) { ... }
Run Code Online (Sandbox Code Playgroud)
然后你可以等待它:
//pCopyFromResourcesToLocal(uFileName);
await pCopyFromResourcesToLocal(uFileName);
Run Code Online (Sandbox Code Playgroud)
它会在你打电话之前完成 return (_dbApp = new DB(path));
此async/ await链中的任何内容都不会发生故障.