Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WP7:将应用程序从墓碑状态恢复会导致应用程序崩溃。使用Writeablebitmap_C#_Wpf_Windows Phone 7 - Fatal编程技术网

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

C# WP7:将应用程序从墓碑状态恢复会导致应用程序崩溃。使用Writeablebitmap,c#,wpf,windows-phone-7,C#,Wpf,Windows Phone 7,我正在使用Writeablebitmap拍摄UI元素的屏幕截图。 代码如下所示: private void Screenshot(FrameworkElement element, String fileNameLoader) { try { WriteableBitmap bmp = new WriteableBitmap(element, null); MemoryStream ms = new

我正在使用Writeablebitmap拍摄UI元素的屏幕截图。 代码如下所示:

    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();
        }
    }
我遇到的问题是,如果我按save按钮调用Screenshot()方法,然后按Home按钮删除应用程序,最后按Back按钮将应用程序恢复,我会看到一个屏幕显示“Resuming…”,应用程序最终崩溃。 在进行一些调试之后,我注意到错误似乎是由以下代码行引起的:

WriteableBitmap bmp = new WriteableBitmap(element, null);
将该行替换为:

    WriteableBitmap bmp = null;
使我免于崩溃,但我的应用程序无法正常工作(屏幕截图无法工作)


有没有人遇到过这个问题,或者知道如何解决它?我对任何解决方法都持开放态度,只要我仍然可以拍摄特定UI元素的屏幕截图。

我无法重现错误,但您可以尝试以下方法:

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

不捕获所有异常如何?尝试用调试器启动它并获得错误。您的UI元素有多复杂?你能运行这个应用程序(不带逻辑删除)并告诉我们新的WriteableBitmap()需要多长时间(使用Environment.TickCount前后进行计算,或者你喜欢的任何方法,但寻找实际时间,而不是cpu时间)lukas,当我使用调试器(在模拟器或实际设备上)运行它时,这个应用程序从未真正崩溃,它只是停留在屏幕上说“恢复…”。已经持续了大约5分钟了。如果我直接在设备上运行它(而不是通过Visual Studio),那么“Resuming…”屏幕会显示大约3-4秒,然后手机会返回开始菜单。Shahar,我按照您的建议使用TickCount计算了时间。我做了5次实验,平均时间为217.4ms。UI元素非常简单,它是两个图像,一个来自相机,一个来自资源文件以及两个文本框。不,使用此代码时的行为是相同的。问题似乎确实出在WriteableBitmap部分。