此WPF程序顯示一個ContextMenu,它包含一個標記爲'Exit'的MenuItem以及一個空的Window。選擇「退出」應該終止進程,但它只關閉窗口和ContextMenu。我並不想強行終止這個計劃,而是乾脆結束它。Application.Shutdown()從ContextMenu失敗
爲什麼在Click事件處理程序中調用Application.Shutdown()無法關閉程序?
using System;
using System.Windows;
using System.Windows.Controls;
class MyApp : Application {
[STAThread]
public static void Main() {
new MyApp().Run();
}
protected override void OnStartup(StartupEventArgs e) {
new Window().Show();
MenuItem menuItem = new MenuItem();
menuItem.Header = "Exit";
menuItem.Click += delegate { Shutdown(); };
ContextMenu contextMenu = new ContextMenu();
contextMenu.Items.Add(menuItem);
contextMenu.IsOpen = true;
}
}
看到這個回答:http://stackoverflow.com/questions/606043/shutting-down-a-wpf-application-from-app-xaml-cs – VinayC 2012-04-06 04:10:54
你的ShutdownMode設置爲什麼? – Flot2011 2012-04-06 04:28:43
ShutdownMode是默認的OnLastWindowClose。 – 2012-04-06 04:45:58