我有一個簡單的WPF應用程序與MainWindow。在後面的代碼中設置一個未加載的事件。將MainWindow
設置爲啓動uri。當窗口關閉時,Unloaded不會被觸發。創建第二個窗口 - NotMainWindow
只需點擊一下按鈕。奇怪的卸載行爲 - WPF窗口
在按鈕單擊事件中,調用MainWindow。關閉MainWindow
並觸發卸載。 爲什麼行爲上的差異?我想知道的是,如何獲得某種空載事件每單次?
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Unloaded="Main_Unloaded">
<Grid>
</Grid>
</Window>
private void Main_Unloaded(object sender, RoutedEventArgs e)
{
}
<Window x:Class="WpfApplication2.NotMainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="NotMainWindow" Height="300" Width="300">
<Grid>
<Button Content="Show Main" Height="25" Margin="10" Width="70" Click="Button_Click" />
</Grid>
</Window>
private void Button_Click(object sender, RoutedEventArgs e)
{
MainWindow win = new MainWindow();
win.Show();
}
<Application x:Class="WpfApplication2.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="NotMainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
它在關閉時未被調用。這是問題。當然,它不會被稱爲加載! :P – Harsha
@Blachshma - 更正了問題以消除混淆。 – Harsha
所以你說你創建MainWindow,然後立即關閉它,卸載不被調用? – Blachshma