我有一个简单的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