MainWindowを備えた単純なWPFアプリケーションがあります。コードビハインドでアンロードイベントを設定します。MainWindow
を起動uriとして設定します。ウィンドウを閉じても、アンロードはトリガーされません。NotMainWindow
ボタンを1回クリックするだけで、2番目のウィンドウを作成します。
ボタンクリックイベントで、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>