WPF(MVVM)主窗口关闭时 弹窗提示是否关闭
程序员文章站
2022-07-13 22:49:42
...
窗口设置为:
Name="MianPage"
WindowState="Maximized"
Closing="Window_Closing"
在主窗口中:
//关闭窗口事件
//不知道与重写窗口关闭事件 OnClosed有什么区别
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//系统自动提示窗
//MessageBoxResult result = MessageBox.Show("是否退出本系统?", "", MessageBoxButton.YesNo);
//if (result == MessageBoxResult.No)
//{
// e.Cancel = true;
//}
//自定义提示窗
//取消窗口关闭
e.Cancel = true;
//跳转到退出窗口
LogDown logdown = new LogDown();
//在父窗口中间显示,
logdown.WindowStartupLocation = WindowStartupLocation.CenterOwner;
logdown.Owner = this;
logdown.Topmost = true;
logdown.ShowDialog();
}
自定义提示窗口:
public partial class LogDown : Window
{
public LogDown()
{
InitializeComponent();
/*窗口固定在屏中*/
WindowStartupLocation = WindowStartupLocation.CenterScreen;
}
//退出系统按钮
private void Button_Click(object sender, RoutedEventArgs e)
{
// 强制退出,即使有其他的线程没有结束
Environment.Exit(0);
// 关闭当前程序,如果有其他线程没有结束,不会关闭
//Application.Current.Shutdown();
}
//热键绑定
private void CommandBinding_CanExecute1(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void CommandBinding_Executed1(object sender, ExecutedRoutedEventArgs e)
{
Button_Click(this, null);
}
//不退出系统按钮
private void Button_Click_1(object sender, RoutedEventArgs e)
{
this.Close();// 关闭当前窗口,可以在OnClosing和 OnClosed中捕获消息,在OnClosing的时候,可以取消关闭窗口。
}
/// <summary>
/// 热键绑定
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CommandBinding_CanExecute2(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void CommandBinding_Executed2(object sender, ExecutedRoutedEventArgs e)
{
Button_Click_1(this, null);
}
}
上一篇: RDIFramework.NET代码生成器全新V5.0版本发布
下一篇: 真随机数