如何在WPF窗口中触发UserControl的Unload事件

20

我在WPF中有一个用户控件:

<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>

当我卸载这个控件时,WPF会触发事件DBLoginUserFrame_Unloaded来保存我的设置并完成相关工作。

我在WPF中有一个MainWindow,它加载了这个用户控件,但是当窗口关闭时,我的用户控件卸载事件没有被触发:

<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>

如何将UserControl的Unload事件添加到MainWindow的事件处理程序中?


看看这个解决方法。尝试使用"new Window();"。 - icernos
3个回答

29

根据 文档,请注意:

“请注意,应用程序开始关闭后不会触发卸载事件。应用程序关闭是在 ShutdownMode 属性定义的条件发生时发生的。如果在 Unloaded 事件处理程序中放置清理代码(例如 Window 或 UserControl 的处理程序),则可能无法按预期调用它。”

如果关闭窗口会触发应用程序的关闭,那可能就是原因。在这种情况下,甚至 Window 的 Unload 事件也可能不会被调用,因此我认为最好依靠 Window.Closing 事件。

处理 UserControl 卸载任务的一种方法是公开控件的 unload 处理程序方法("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().
}

另一个选择是保持你目前的实现,但也在你的UserControl中添加一个处理程序来处理Dispatcher.ShutdownStarted事件,如这里所描述的。

public MyUserControl()
{
    this.Dispatcher.ShutdownStarted += Dispatcher_ShutdownStarted;
}

不使用库的用户控件。我不能相信控件的使用者来处理这个问题。Dispatcher.ShutdownStarted解决方案对我来说非常完美。 - Jamie

11

你也可以将逻辑放入用户控件本身中,以使其更加自包含。

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-closing?forum=wpf


-1

看看这个解决方法

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        new Window(); //<-- this will make Unloaded Event to trigger in WPF
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接