1、WPF应用程序添加splashScreen(初始屏幕)
(1)跟目录导入图片
(2)在App.xaml.cs文件中输入以下代码

protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
SplashScreen splashScreen = new SplashScreen("EzGUwC4VEAIxi6E.jpg");
splashScreen.Show(true);
}
2、设置应用程序的初始窗口
(1)在以下输入StartupUri=”MainWindow.xaml” 是初始窗口的文件名

<Application x:Class="DP.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DP"
ShutdownMode="OnMainWindowClose"
StartupUri="MainWindow.xaml">
</Application>
3、设置应用程序的关闭方式
(1)在以下输入ShutdownMode=”OnMainWindowClose”关闭模式,一种有3种 OnlastWindowClose、OnExplicitShutdown(强制调用 Application.shoutdown)、OnMainWindowClose
<Application x:Class="DP.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DP"
ShutdownMode="OnMainWindowClose"
StartupUri="MainWindow.xaml">
</Application>
4、应用程序的窗体退出设置
在App.xaml.cs文件中输入以下代码
protected override void OnExit(ExitEventArgs e)
{
base.OnExit(e);
MessageBoxResult messageBoxResult = MessageBox.Show("dfsf");
MessageBox.Show("sdfsfsd");
}
5、计算机注销或关键结束应用程序时设置
在App.xaml.cs文件中输入以下代码
protected override void OnSessionEnding(SessionEndingCancelEventArgs e)
{
base.OnSessionEnding(e);
MessageBoxResult messageBoxResult = MessageBox.Show("dfsf");
MessageBox.Show("sdfsfsd");
if(messageBoxResult == MessageBoxResult.No)
e.Cancel = true;
}
6、记录应用程序未处理异常
DispatcherUnhandedException事件
// 程序启动时注册DispatcherUnhandledException事件
protected override void OnStartup(StartupEventArgs e)
{
Current.DispatcherUnhandledException += AppDispatcherUnhandledException;
base.OnStartup(e);
}
protected override void OnExit(ExitEventArgs e)
{
Current.DispatcherUnhandledException -= AppDispatcherUnhandledException;
base.OnExit(e);
}
private void AppDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
//设置异常已处理
e.Handled = true;
//异常处理
new ExceptionViewModel(e.Exception);
}
原创文章,作者:254126420,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/276580.html