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

时间:2015-04-04 08:35:46

标签: c# .net wpf

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

2 个答案:

答案 0 :(得分:0)

您可以创建一个从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>

答案 1 :(得分:0)

我想注册附属物:

  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>