小编m0n*_*ter的帖子

如何找出 EventProperty 名称?

我的 C# 应用程序订阅 Windows 事件日志消息:

var subscriptionQuery = new EventLogQuery(Settings.Default.LogPath, PathType.LogName, Settings.Default.LogQuery);
_watcher = new EventLogWatcher(subscriptionQuery);
_watcher.EventRecordWritten += EventLogEventRead;
Run Code Online (Sandbox Code Playgroud)

当消息发生时,EventLogEventRead处理程序接收一个System.Diagnostics.Eventing.Reader.EventLogRecord包含事件数据的对象。此信息包括EventProperty对象的集合。问题是:EventProperty只定义了一个值,而不是属性的名称。但是,当我在 Windows 事件日志查看器中打开相同的事件时,它会显示带有名称的属性。现在的问题是:如何获取事件属性名称?

.net c# windows event-log

6
推荐指数
2
解决办法
2578
查看次数

具有 DependencyProperty 的最简单 WPF UserControl 中的内存泄漏

因此,我尝试创建一个具有一个新依赖属性并使用此 DP 的 WPF 控件。该控件的 XAML 非常简单:

<UserControl x:Class="DPTest.TestControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:DPTest">
<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=local:TestControl}, Path=TestText}"/>
Run Code Online (Sandbox Code Playgroud)

只是一个显示新属性的 TextBlock。Codebehind 声明依赖属性如下:

using System.Windows;

namespace DPTest
{
  public partial class TestControl
  {
    public static readonly DependencyProperty TestTextProperty = 
      DependencyProperty.Register("TestText", typeof(string), typeof(TestControl), new PropertyMetadata(default(string)));

    public TestControl()
    {
      InitializeComponent();
    }

    public string TestText
    {
      get => (string) GetValue(TestTextProperty);
      set => SetValue(TestTextProperty, value);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

到目前为止一切都很好。但是,创建此控件永远不会被 GC 收集。为了演示这一点,我使用一个窗口,它在加载时简单地创建大量 TestControl:

using System.Windows;

namespace DPTest
{
  public partial class MainWindow
  {
    public MainWindow()
    {
      InitializeComponent();
      Loaded …
Run Code Online (Sandbox Code Playgroud)

c# wpf memory-leaks dependency-properties

5
推荐指数
1
解决办法
2015
查看次数