我一直在思考同样的问题,并在搜索过程中很早就偶然发现了这个。我已经找到了一种方法,希望对其他人也有用。
问题在于await
返回UI线程控制权并导致应用程序崩溃。您需要延迟,但实际上没有什么好方法可以获得。
我的解决方案是改用设置存储。我假设大多数想要这样做的人都想要像LittleWatson一样做些什么,因此以下代码是从http://blogs.msdn.com/b/andypennell/archive/2010/11/01/error-reporting-on-windows-phone-7.aspx修改而来,以方便使用:
namespace YourApp
{
using Windows.Storage;
using Windows.UI.Popups;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
public class LittleWatson
{
private const string settingname = "LittleWatsonDetails";
private const string email = "mailto:?to=you@example.com&subject=YourApp auto-generated problem report&body=";
private const string extra = "extra", message = "message", stacktrace = "stacktrace";
internal static void ReportException(Exception ex, string extraData)
{
ApplicationData.Current.LocalSettings.CreateContainer(settingname, Windows.Storage.ApplicationDataCreateDisposition.Always);
var exceptionValues = ApplicationData.Current.LocalSettings.Containers[settingname].Values;
exceptionValues[extra] = extraData;
exceptionValues[message] = ex.Message;
exceptionValues[stacktrace] = ex.StackTrace;
}
internal async static Task CheckForPreviousException()
{
var container = ApplicationData.Current.LocalSettings.Containers;
try
{
var exceptionValues = container[settingname].Values;
string extraData = exceptionValues[extra] as string;
string messageData = exceptionValues[message] as string;
string stacktraceData = exceptionValues[stacktrace] as string;
var sb = new StringBuilder();
sb.AppendLine(extraData);
sb.AppendLine(messageData);
sb.AppendLine(stacktraceData);
string contents = sb.ToString();
SafeDeleteLog();
if (stacktraceData != null && stacktraceData.Length > 0)
{
var dialog = new MessageDialog("A problem occured the last time you ran this application. Would you like to report it so that we can fix the error?", "Error Report")
{
CancelCommandIndex = 1,
DefaultCommandIndex = 0
};
dialog.Commands.Add(new UICommand("Send", async delegate
{
var mailToSend = email.ToString();
mailToSend += contents;
var mailto = new Uri(mailToSend);
await Windows.System.Launcher.LaunchUriAsync(mailto);
}));
dialog.Commands.Add(new UICommand("Cancel"));
await dialog.ShowAsync();
}
}
catch (KeyNotFoundException)
{
}
}
private static void SafeDeleteLog()
{
ApplicationData.Current.LocalSettings.CreateContainer(settingname, Windows.Storage.ApplicationDataCreateDisposition.Always);
var exceptionValues = ApplicationData.Current.LocalSettings.Containers[settingname].Values;
exceptionValues[extra] = string.Empty;
exceptionValues[message] = string.Empty;
exceptionValues[stacktrace] = string.Empty;
}
}
}
为了实现它,您需要按照上面链接中的说明进行操作,但是为了确保数据在URL失效时仍然存在,需要执行以下操作:
App.xaml.cs 构造函数(在调用 this.InitializeComponent() 之前):
this.UnhandledException += (s, e) => LittleWatson.ReportException(e.Exception, "extra message goes here")
如果您已经有了一个UnhandledException方法,您可以在其中抛出对LittleWatson的调用。
如果您使用的是Windows 8.1,您还可以添加一个NavigationFailed调用。这需要在实际页面中进行(通常是MainPage.xaml.cs或任何首先打开的页面):
xx.xaml.cs
构造函数(任何给定页面):
rootFrame.NavigationFailed += (s, e) => LittleWatson.ReportException(e.Exception, "extra message goes here")
最后,您需要询问用户在应用重新打开时是否要发送电子邮件。在您的应用程序的默认页面构造函数中(默认情况下,页面App.xaml.cs 初始化):
this.Loaded += async (s, e) => await LittleWatson.CheckForPreviousException()
如果您已经使用了OnLoad方法,请将此调用添加到其中;否则,请在代码中直接添加。