dav*_*rtu 15 wpf events xaml user-controls routedevents
我在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>
Run Code Online (Sandbox Code Playgroud)
当我卸载此控件时,WPF引发事件DBLoginUserFrame_Unloaded,保存我的设置,它完成一项工作.
我在WPF中的MainWindow加载了这个用户控件但是当窗口关闭时,我的usercontrol 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>
Run Code Online (Sandbox Code Playgroud)
如何将UserControl Unload事件添加到MainWindow事件处理程序?
Art*_*nes 21
从文档:
"请注意,应用程序开始关闭后不会引发Unloaded事件.当ShutdownMode属性定义的条件发生时,将发生应用程序关闭.如果将清理代码放在Unloaded事件的处理程序中,例如Window或UserControl ,它可能没有按预期调用."
如果关闭Window会触发应用程序的关闭,那可能就是原因.在这种情况下,甚至可能也不会调用Window的Unload事件,所以我认为最好依赖Window.Closing事件.
处理卸载时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().
}
Run Code Online (Sandbox Code Playgroud)
另一种选择是让你实现到目前为止也可加在你的用户控件的Dispatcher.ShutdownStarted事件的处理程序,如所描述这里.
public MyUserControl()
{
this.Dispatcher.ShutdownStarted += Dispatcher_ShutdownStarted;
}
Run Code Online (Sandbox Code Playgroud)
小智 9
您还可以将逻辑放入用户控件本身以使其更加独立。
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
}
Run Code Online (Sandbox Code Playgroud)
在这里看到:https : //social.msdn.microsoft.com/Forums/vstudio/en-US/477e7e74-ccbf-4498-8ab9-ca2f3b836597/how-to-know-when-a-wpf-usercontrol-is-关闭?论坛=wpf