C#W10 UWP BackgroundTask存储文件
我正在尝试在BackgroundTask
的C#W10 UWP应用中将图像设置为锁屏或墙纸...在正常执行中,我可以做到这一点,但是当我将相同的代码放入StorageFile.CreateStreamedFileFromUriAsync
.
I'm trying to set an image to lockscreen or wallpaper in my C# W10 UWP app in a BackgroundTask
... I can do this just fine in normal execution, but when I put the same code into a BackgroundTask
, the code hangs at StorageFile.CreateStreamedFileFromUriAsync
.
// See if file exists already, if so, use it, else download it
StorageFile file = null;
try {
file = await ApplicationData.Current.LocalFolder.GetFileAsync(name);
} catch (FileNotFoundException) {
if (file == null) {
Debug.WriteLine("Existing file not found... Downloading from web");
file = await StorageFile.CreateStreamedFileFromUriAsync(name, uri, RandomAccessStreamReference.CreateFromUri(uri)); // hangs here!!
file = await file.CopyAsync(ApplicationData.Current.LocalFolder);
}
}
// Last fail-safe
if (file == null) {
Debug.WriteLine("File was null -- error finding or downloading file...");
} else {
// Now set image as wallpaper
await UserProfilePersonalizationSettings.Current.TrySetLockScreenImageAsync(file);
}
我不知道BackgroundTasks
中对StorageFile
的任何限制吗?一些谷歌搜索没有产生这样的限制...
Are there any restrictions to StorageFile
in BackgroundTasks
that I'm unaware of? Some googling yielded no such restrictions...
有什么想法吗?
谢谢.
代码挂起"是什么意思?例外?开始操作,但不会完成/返回吗? 我遇到了(或正在解决)类似问题,或者至少我认为这是类似问题.
What does "the code hangs" mean? Exception? Starts the operation but wont finish/return? I had (or have, I'm still working on) a similar problem, or at least I think it's similar.
也许是异步配置上下文问题.
Maybe it's a async-configure-context problem.
//not tested...but try ...AsTask().ConfigureAwait(false):
file = await StorageFile.CreateStreamedFileFromUriAsync(name, uri, RandomAccessStreamReference.CreateFromUri(uri)).AsTask().ConfigureAwait(false);
启动,但不会返回(未击中下一行代码的断点),听起来仍然像是SynchronizationContext/Deadlock问题...嗯...
Starts, but wont return (breakpoint on next line of code not hit), sounds still like a SynchronizationContext/Deadlock problem... hmm...
您是否已检查(或确保)CreateStreamedFileFromUriAsync
确实完成了?
Have you checked (or make sure) that CreateStreamedFileFromUriAsync
really finishes?