如何在App WPF中追踪所有Show或ShowDialog



我有一个要求,即要跟踪WPF中的所有Window.ShowShowDialog()。主要目的是我想知道应用程序中的所有窗口何时打开或关闭。类似于,当关闭WindowA或ChildWindowA时,我想编写打开/关闭视图的AuditLog,我不想为每个窗口或ChildWindow编写代码,并在应用程序实例级别编写代码来处理应用程序中所有打开/关闭的窗口或ChildWindow。

您可以创建一个从Window派生的基类来处理日志记录。

public class AuditLoggableWindow : Window
{
    public AuditLoggableWindow()
    {
        Closing += OnClosing;
        ContentRendered += OnShown;
    }
    protected void OnClosing(object o, CancelEventArgs e)
    {
        // log that window is closing now
    }
    protected void OnShown(object o, EventArgs e)
    {
        // log that the window has been shown
    }
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : AuditLoggableWindow
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

在XAML标记中,您需要将Window标记替换为namespace:AuditLoggableWindow。由于我的项目的名称空间是wpfApplication1,因此标记如下:

<wpfApplication1:AuditLoggableWindow x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:wpfApplication1="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
    </Grid>
</wpfApplication1:AuditLoggableWindow>

我想注册一个附加的属性:

  public static class WindowLog
{
    public static readonly DependencyProperty EnableLogProperty =
        DependencyProperty.RegisterAttached(
            "EnableLog",
            typeof(bool),
            typeof(WindowLog),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.None, OnEnableWindowLogChanged));
    public static void SetEnableWindowLog(Window window, bool value)
    {
        window.SetValue(EnableLogProperty, value);
    }
    public static bool GetEnableWindowLog(Window element)
    {
        return (bool)element.GetValue(EnableLogProperty);
    }
    private static void OnEnableWindowLogChanged(
        DependencyObject dependencyObject,
        DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
    {
        Window window = dependencyObject as Window;
        if (window == null)
        {
            return;
        }
        if (GetEnableWindowLog(window))
        {
            Register(window);
        }
        else
        {
            Unregister(window);
        }  
    }
    private static void Unregister(Window window)
    {
        window.Closing -= Window_Closing;
        window.Activated -= Window_Activated;
        window.Closed -= Window_Closed;
    }
    private static void Register(Window window)
    {
        window.Closing += Window_Closing;
        window.Activated += Window_Activated;
        window.Closed += Window_Closed;
    }
    private static void Window_Closed(object sender, EventArgs e)
    {
        Window window = (Window)sender;     
        window.Closing -= Window_Closing;
        window.Activated -= Window_Activated;
        window.Closed -= Window_Closed;
    }
    private static void Window_Activated(object sender, EventArgs e)
    {  
        // do something
    }
    private static void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        // do something
    } 
}

使用

<Window x:Class="Wpf.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:attachments="clr-namespace:Wpf.Attachments"
    attachments:WindowLog.EnableWindowLog="true">
<StackPanel>
</StackPanel>

最新更新