简体   繁体   English

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

[英]Windows Phone 8.1 - Exception when creating WriteableBitmap from a Background Task

I'm building a Windows Phone 8.1 app (Windows Runtime, not Windows Phone Silverlight 8.1). 我正在构建Windows Phone 8.1应用程序(Windows运行时,而不是Windows Phone Silverlight 8.1)。 I have created a Background Task that I trigger by using a Maintenance Trigger. 我创建了一个后台任务,该任务通过使用维护触发器来触发。 Inside the background task, I need to create a WriteableBitmap from one picture of Camera Roll. 在后台任务中,我需要从“相机胶卷”的一张图片创建一个WriteableBitmap。 My code is as follows: 我的代码如下:

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();
    }
}

When I run the background task, the two notifications work as expected, but I'm getting the following exceptions: 当我运行后台任务时,两个通知均按预期工作,但出现以下异常:

"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)"

If I remove this lines: 如果我删除以下行:

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

Everything works as expected, the 2 notifications appear and no exception is thrown. 一切正常,显示2条通知,并且不会引发异常。

Any ideas? 有任何想法吗?

Thank you. 谢谢。

Use XamlRenderingBackgroundTask in order to generate XAML graphics in the background. 使用XamlRenderingBackgroundTask在后台生成XAML图形。 Note that it is recommended you use C++ for this in order to minimize memory usage. 请注意,建议您为此使用C ++,以最大程度地减少内存使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM