简体   繁体   中英

How to track down all Show or ShowDialog in App WPF

I has a requirement that to track down all Window.Show or ShowDialog() in WPF. The main purpose is I want to know when all Window in App open or close. Something like, when closing WindowA or ChildWindowA, I want to write AuditLog for which view was opened/closed, I don't want to write code for each Window or ChildWindow and write it in App instance level to handle all open/close Window or ChildWindow in App.

You could create a base class deriving from Window that handles the logging.

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();

    }
}

And in your XAML markup you need to Replace the Window tag with namespace:AuditLoggableWindow . As the namespace of my project is wpfApplication1 , the markup would be as follows:

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

I would like to register an attached property:

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

Using

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM