[英]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. 我有一个要求,以跟踪WPF中的所有Window.Show
或ShowDialog()
。 The main purpose is I want to know when all Window in App open or close. 主要目的是我想知道App中的所有Window何时打开或关闭。 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. 像这样,当关闭WindowA或ChildWindowA时,我想编写为其视图打开/关闭的AuditLog,我不想为每个Window或ChildWindow编写代码并在App实例级别编写它以处理所有打开/关闭Window或App中的ChildWindow。
You could create a base class deriving from Window
that handles the logging. 您可以创建一个从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();
}
}
And in your XAML markup you need to Replace the Window
tag with namespace:AuditLoggableWindow
. 在XAML标记中,您需要将Window
标记替换为namespace:AuditLoggableWindow
。 As the namespace of my project is wpfApplication1
, the markup would be as follows: 由于我项目的名称空间是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>
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.