Windows Phone 8.1 - 从后台任务创建WriteableBitmap时的异常

时间:2015-01-18 15:23:28

标签: c# windows-runtime windows-phone windows-phone-8.1 background-task

我正在构建Windows Phone 8.1应用程序(Windows运行时,而不是Windows Phone Silverlight 8.1)。我创建了一个后台任务,我使用维护触发器触发。在后台任务中,我需要从Camera Roll的一张图片创建一个WriteableBitmap。我的代码如下:

public sealed class Class1 : IBackgroundTask
{
    public async void Run(IBackgroundTaskInstance taskInstance)
    {
        BackgroundTaskDeferral deferral = taskInstance.GetDeferral();

        var files = await KnownFolders.CameraRoll.GetFilesAsync();

        ShowNotification("Process has started");

        using(var fileStream = await files[0].OpenStreamForReadAsync())
        {
            WriteableBitmap writeableBitmap = await BitmapFactory.New(1, 1).FromStream(fileStream.AsRandomAccessStream());
        }

        ShowNotification("Process has ended");

        deferral.Complete();
    }
}

当我运行后台任务时,这两个通知按预期工作,但我得到以下例外:

"A first chance of exception of type "System.Exception" occurred in WriteableBitmapEx.WinRT.DLL""
"A first chance of exception of type "System.Exception" occurred in mscorlib.dll"

"The proccess has ended with code 1 (0x1)"

如果我删除这一行:

using(var fileStream = await files[0].OpenStreamForReadAsync())
{
    WriteableBitmap writeableBitmap = await BitmapFactory.New(1, 1).FromStream(fileStream.AsRandomAccessStream());
}

一切都按预期工作,出现2个通知,不会抛出任何异常。

有什么想法吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

使用XamlRenderingBackgroundTask以在后台生成XAML图形。请注意,建议您使用C ++来最小化内存使用。