我想在本地目录上存储bitmapimages。我写了那些代码。但是发生了一个未知的错误,无法编译。请告诉我错误的原因以及转换和存储位图图像的正确方法。
void StoreAndGetBitmapImage()
{
BitmapImage image = new BitmapImage(new Uri("ms-appx:///Assets/" + "test.png"));
StorageFile storageFile = ConvertBitmapImageIntoStorageFile(image, "image_name");
StoreStorageFile(storageFile);
BitmapImage resultImage = GetBitmapImage("image_name");
}
StorageFile ConvertBitmapImageIntoStorageFile(BitmapImage bitmapImage,string fileName)
{
StorageFile file = Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(bitmapImage.UriSource).GetResults();
file.RenameAsync(fileName);
return file;
}
void StoreStorageFile(StorageFile storageFile)
{
storageFile.CopyAsync(Windows.Storage.ApplicationData.Current.LocalFolder);
}
BitmapImage GetBitmapImage(string fileName)
{
BitmapImage bitmapImage;
bitmapImage = new BitmapImage();
bitmapImage.UriSource = new Uri(new Uri(
Windows.Storage.ApplicationData.Current.LocalFolder.Path + "\\" +
Windows.Storage.ApplicationData.Current.LocalFolder.Name),
fileName);
return bitmapImage;
}
答案 0 :(得分:0)
您需要await
异步方法调用。因此,您必须将该方法声明为async。例如:
async Task<StorageFile> ConvertBitmapImageIntoStorageFile(BitmapImage bitmapImage,string fileName)
{
StorageFile file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(bitmapImage.UriSource);
await file.RenameAsync(fileName);
return file;
}
await
导致从方法返回。如果任务完成,则该方法在此位置继续(可能在不同的线程中)。异步方法返回IAsyncOperation
个对象,例如一个Task
。这是启动过程的句柄,可用于确定何时完成。