0

我正在构建一个 Windows Phone 8.1 应用程序(Windows 运行时,而不是 Windows Phone Silverlight 8.1)。我创建了一个使用维护触发器触发的后台任务。在后台任务中,我需要从一张相机胶卷图片创建一个 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 个通知并且没有抛出异常。

有任何想法吗?

谢谢你。

4

1 回答 1

0

用于XamlRenderingBackgroundTask在后台生成 XAML 图形。请注意,建议您为此使用 C++,以最大限度地减少内存使用。

于 2015-01-19T16:05:55.653 回答