简体   繁体   English

WP7:从墓碑状态恢复应用程序导致应用程序崩溃。 使用Writeablebitmap

[英]WP7: Bringing Back App from a Tombstone State Causes App to Crash. Using a Writeablebitmap

I'm using a Writeablebitmap to take a screenshot of a UI element. 我正在使用Writeablebitmap来截取UI元素的截图。 The code looks like this: 代码如下所示:

    private void Screenshot(FrameworkElement element, String fileNameLoader)
    {
        try
        {
            WriteableBitmap bmp = new WriteableBitmap(element, null);

            MemoryStream ms = new MemoryStream();
            bmp.SaveJpeg(ms, (int)element.ActualWidth, (int)element.ActualHeight, 0, 100);
            ms.Seek(0, SeekOrigin.Begin);

            MediaLibrary lib = new MediaLibrary();
            String filePath = string.Format(fileNameLoader);
            lib.SavePicture(filePath, ms);                
         }
        catch (Exception exception)
        {
            txtDebug.Text = "There was an error. Could not save. " + exception.ToString();
        }
    }

The issue I am having is that if I press the save button which calls the Screenshot() method, then press the Home button to tombstone the app and finally press the Back button to bring the app back, I get a screen that says "Resuming..." and the app eventually crashes. 我遇到的问题是,如果我按下调用屏幕截图()方法的保存按钮,然后按主页按钮以删除应用程序,最后按返回按钮将应用程序恢复,我会看到一个屏幕显示“正在恢复......“而应用程序最终崩溃了。 After doing some debugging I've noticed that the error seems to be caused by the following line of code: 做了一些调试后我发现错误似乎是由以下代码行造成的:

WriteableBitmap bmp = new WriteableBitmap(element, null);

Replacing that line with: 用以下内容替换该行:

    WriteableBitmap bmp = null;

saves me from the crash but my app does not work as intended (screenshot doesn't work). 让我免于崩溃,但我的应用程序无法正常工作(截图不起作用)。

Has anyone ever encountered this issue or would know how to fix it? 有没有人遇到过这个问题或者知道如何修复它? I'm open to any work-around, as long as I can still take a screenshot of a specific UI element. 我愿意接受任何解决方案,只要我仍然可以截取特定UI元素的屏幕截图。

I can't reproduce error but you may try this: 我无法重现错误,但你可以试试这个:

private void Screenshot(FrameworkElement element, String fileNameLoader)
{
    WriteableBitmap bmp = new WriteableBitmap(element, null);
    using (MemoryStream ms = new MemoryStream())
    {
        bmp.SaveJpeg(ms, (int)element.ActualWidth, (int)element.ActualHeight, 0, 100);
        ms.Seek(0, SeekOrigin.Begin);
        using (MediaLibrary lib = new MediaLibrary())
        {
            String filePath = string.Format(fileNameLoader);
            lib.SavePicture(filePath, ms);
        }
    }
}

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

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