19

我在 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,但是当窗口关闭时,我的用户控件 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>

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

4

3 回答 3

28

文档中:

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

如果关闭窗口会触发应用程序的关闭,这可能是原因。在这种情况下,即使是 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().
}

另一种选择是保持您的实现到目前为止,但也在您的 UserControl 中添加 Dispatcher.ShutdownStarted 事件的处理程序,如此处所述

public MyUserControl()
{
    this.Dispatcher.ShutdownStarted += Dispatcher_ShutdownStarted;
}
于 2013-01-23T12:50:41.210 回答
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
}

在这里看到这个: https ://social.msdn.microsoft.com/Forums/vstudio/en-US/477e7e74-ccbf-4498-8ab9-ca2f3b836597/how-to-know-when-a-wpf-usercontrol-is-关闭?论坛=wpf

于 2018-05-11T11:16:44.133 回答
-1

看看这个解决方法

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

        new Window(); //<-- this will make Unloaded Event to trigger in WPF
    }
}
于 2019-07-19T23:37:36.573 回答