如何在 WPF 窗口中触发 Usercontrol 的 Unload 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14479038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fire Unload event of Usercontrol in a WPF window
提问by davymartu
I have an UserControl in WPF:
我在 WPF 中有一个 UserControl:
<UserControl x:Class="XLogin.DBLogin"
x:Name="DBLoginUserFrame"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Height="263"
Width="353"
Loaded="DBLoginUserFrame_Loaded"
Unloaded="DBLoginUserFrame_Unloaded">
<Grid>
<GroupBox Header="Database Connection"
HorizontalAlignment="Left"
Height="243"
Margin="10,10,0,0"
VerticalAlignment="Top"
Width="333">
<Grid>
<TextBox x:Name="TextUserDB"
HorizontalAlignment="Left"
Height="20"
Margin="101,60,0,0"
TextWrapping="Wrap"
VerticalAlignment="Top"
Width="173" />
<Label Content="Password:"
HorizontalAlignment="Left"
Height="24"
VerticalAlignment="Top"
Width="70"
HorizontalContentAlignment="Right"
Margin="10,85,0,0" />
<PasswordBox x:Name="TextPasswordDB"
HorizontalAlignment="Left"
Height="20"
Margin="101,89,0,0"
VerticalAlignment="Top"
Width="173" />
<Button x:Name="BtnConnect"
Content="Connetti"
Height="29"
Width="123"
Margin="101,152,97,24"
Click="BtnConnect_Click" />
</Grid>
</GroupBox>
</Grid>
</UserControl>
When i Unload this Control, WPF raise event DBLoginUserFrame_Unloadedthat save my settings and it does a job.
当我卸载此控件时,WPF 会引发事件DBLoginUserFrame_Unloaded以保存我的设置并完成工作。
I have the MainWindow in WPF that load this user control but when window is closed, my usercontrol UNLOAD doesn't fire:
我在 WPF 中有加载此用户控件的 MainWindow,但是当窗口关闭时,我的用户控件 UNLOAD 不会触发:
<Window x:Class="XLogin.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"
xmlns:local="clr-namespace:XLogin" Unloaded="Window_Unloaded_1">
<Grid>
<local:DBLogin/>
</Grid></Window>
How to add the UserControl Unload event to MainWindow event handler?
如何将 UserControl Unload 事件添加到 MainWindow 事件处理程序?
回答by Arthur Nunes
From the documentation:
从文档:
"Note that the Unloaded event is not raised after an application begins shutting down. Application shutdown occurs when the condition defined by the ShutdownMode property occurs. If you place cleanup code within a handler for the Unloaded event, such as for a Window or a UserControl, it may not be called as expected."
“请注意,在应用程序开始关闭后不会引发 Unloaded 事件。当 ShutdownMode 属性定义的条件发生时,应用程序关闭。如果将清理代码放置在 Unloaded 事件的处理程序中,例如用于 Window 或 UserControl ,它可能不会按预期调用。”
If closing your Window triggers your application's shutdown, that might be the cause. In that case, even the Window's Unload event might not be called as well, so I believe it's better to rely on the Window.Closingevent.
如果关闭窗口触发了应用程序的关闭,这可能是原因。在这种情况下,即使是 Window 的 Unload 事件也可能不会被调用,所以我认为最好依赖Window.Closing事件。
One approach to handle the tasks your UserControl does when unloaded is to expose the unload handler method of the control ("DBLoginUserFrame_Unloaded"), name your UserControl instance in the MainWindow and call it from the Window.Closing event.
一种处理 UserControl 在卸载时执行的任务的方法是公开控件的卸载处理程序方法(“DBLoginUserFrame_Unloaded”),在 MainWindow 中命名 UserControl 实例并从 Window.Closing 事件调用它。
public MainWindow()
{
// Add this
this.Closing += MainWindow_Closing;
}
void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
this.MyUserControl.MethodToBeCalledWhenUnloaded().
}
Another option is to keep your implementation so far but also add in your UserControl a handler for the Dispatcher.ShutdownStarted event, as described here.
另一种选择是保留您的实现,但也在您的 UserControl 中添加 Dispatcher.ShutdownStarted 事件的处理程序,如here所述。
public MyUserControl()
{
this.Dispatcher.ShutdownStarted += Dispatcher_ShutdownStarted;
}
回答by tomasat
You could also put the logic into the user control itself to keep it a little more self contained.
您还可以将逻辑放入用户控件本身以使其更加独立。
public UserControl1()
{
InitializeComponent();
this.Loaded += UserControl1_Loaded;
}
void UserControl1_Loaded(object sender, RoutedEventArgs e)
{
Window window = Window.GetWindow(this);
window.Closing += window_Closing;
}
void window_Closing(object sender, global::System.ComponentModel.CancelEventArgs e)
{
//Save your settings here
}
在这里看到:https: //social.msdn.microsoft.com/Forums/vstudio/en-US/477e7e74-ccbf-4498-8ab9-ca2f3b836597/how-to-know-when-a-wpf-usercontrol-is-关闭?论坛=wpf
回答by icernos
Check out this work-around.
看看这个变通方法。
public partial class Window1 : Window { public Window1() { InitializeComponent(); new Window(); //<-- this will make Unloaded Event to trigger in WPF } }
public partial class Window1 : Window { public Window1() { InitializeComponent(); new Window(); //<-- this will make Unloaded Event to trigger in WPF } }