MainWindow.xaml中,添加Closing="MetroWindow_Closing"

 

在窗体的属性为Closing事件绑定方法

 

MainWindow.xaml.cs中

      using System.Threading;


     #region 窗体关闭 /// <summary> /// 窗体关闭 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MetroWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) { e.Cancel = true; CancellationToken token; TaskScheduler uiSched = TaskScheduler.FromCurrentSynchronizationContext(); Task.Factory.StartNew(DialogsBeforeExit, token, TaskCreationOptions.None, uiSched); }


/// <summary> /// 关闭窗体之前的提示对话框 /// </summary> private async void DialogsBeforeExit() { MessageDialogResult result = await this.ShowMessageAsync(this.Title, "您真的要离开吗?", MessageDialogStyle.AffirmativeAndNegative); if (result == MessageDialogResult.Negative) { return; } else//确认退出 {
System.Environment.Exit(System.Environment.ExitCode);
//关闭程序 } } #endregion

 

posted on 2018-08-21 09:51  longzhankunlun  阅读(285)  评论(0编辑  收藏  举报