分类: 嵌入式
2010-11-26 12:55:39
Customizing App.xaml.cs
In the current Beta dev tools for Windows Phone 7, there is no way to exit a Silverlight application (though there is a method to do this for XNA apps).
Currently the best way – and I use the term “best” loosely – to do this is to throw an exception. An unhandled exception always manages to kill your application.
If we must use a hack, however, we might as well do it with a bit of finesse. Below is a simple implementation that can be placed in the App class of a WP7 Silverlight project that clarifies the behavior of the app when we intentionally throw an exception in order to exit rather than simply throwing an exception exceptionally.
This code can be called from anywhere in your app in order to Exit predictably.
Here is the call:
App.Quit();
And here is the implementation in App.xaml.cs:
private class QuitException : Exception { }
public static void Quit()
{
throw new QuitException();
}
private void Application_UnhandledException(object sender,
ApplicationUnhandledExceptionEventArgs e)
{
if (e.ExceptionObject is QuitException)
return;
if (System.Diagnostics.Debugger.IsAttached)
{
System.Diagnostics.Debugger.Break();
}
//etc.
}