C#W10 UWP BackgroundTask存储文件 [英] C# W10 UWP BackgroundTask StorageFile

查看:93
本文介绍了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?

这篇关于C#W10 UWP BackgroundTask存储文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆