gpt4 book ai didi

c# - 如何分配全局初始化事件?

转载 作者:可可西里 更新时间:2023-11-01 08:47:04 24 4
gpt4 key购买 nike

我的 App.xaml.cs 中有这段代码:

protected override void OnStartup(StartupEventArgs e)
{
EventManager.RegisterClassHandler(typeof(TextBox), TextBox.TextChangedEvent, new RoutedEventHandler(TextBox_TextChangedEvent));
}
private void TextBox_TextChangedEvent(object sender, RoutedEventArgs e)
{
// Works
}

我想为 InitializedEvent 做一些类似的事情。
这是我失败的尝试:

protected override void OnStartup(StartupEventArgs e)
{
EventManager.RegisterClassHandler(typeof(FrameworkElement), FrameworkElement.InitializedEvent, new EventHandler(FrameworkElement_InitializedEvent));
}
private void FrameworkElement_InitializedEvent(object sender, EventArgs e)
{

}

InitializedEvent 在别的地方吗?
这可能吗?

我试过使用 LoadedEvent:

protected override void OnStartup(StartupEventArgs e)
{
EventManager.RegisterClassHandler(typeof(FrameworkElement), FrameworkElement.LoadedEvent, new RoutedEventHandler(FrameworkElement_LoadedEvent));
}
private void FrameworkElement_LoadedEvent(object sender, RoutedEventArgs e)
{
// Fires only for Windows
}

它只针对 Windows 而不是 Windows 内部的控件触发。我确实意识到了;当我将加载的事件添加到我的窗口中的标签时;全局 FrameworkElement_LoadedEvent 为该 Label 触发,即使我的正常加载事件 (我专门为 Label 制作的) 是空的。我也试过这些:

EventManager.RegisterClassHandler(typeof(Button), Button.LoadedEvent, new RoutedEventHandler(Button_LoadedEvent));
EventManager.RegisterClassHandler(typeof(Grid), Grid.LoadedEvent, new RoutedEventHandler(Grid_LoadedEvent));
EventManager.RegisterClassHandler(typeof(DataGrid), DataGrid.LoadedEvent, new RoutedEventHandler(DataGrid_LoadedEvent));

但除非我在这些控件上专门添加另一个空加载事件,否则它们不会触发。

我的目标是为每个初始化的控件建立一种时间日志。

如果不在我拥有的每个控件上添加加载的事件,我如何才能实现这一点?

(我有很多)

最佳答案

给你!

public partial class App : Application
{
// ##############################################################################################################################
// Constructor
// ##############################################################################################################################

#region Constructor

static App()
{
// set MyInitialized=true for new windows (happens before Loaded)
EventManager.RegisterClassHandler(typeof(Window), FrameworkElement.SizeChangedEvent, new RoutedEventHandler(OnSizeChanged));

// our loaded handler
EventManager.RegisterClassHandler(typeof(UIElement), FrameworkElement.LoadedEvent, new RoutedEventHandler(OnLoaded), true);
EventManager.RegisterClassHandler(typeof(ContentElement), FrameworkContentElement.LoadedEvent, new RoutedEventHandler(OnLoaded), true);
}

private static void OnSizeChanged(object sender, RoutedEventArgs e)
{
//Console.WriteLine("SizeChanged {0}", sender);
SetMyInitialized((Window) sender, true);
}

private static void OnLoaded(object sender, RoutedEventArgs e)
{
Trace.WriteLine($"{DateTime.Now:O}: {sender} loaded");
}

#endregion

// ##############################################################################################################################
// MyInitialized
// ##############################################################################################################################

#region MyInitialized

public static void SetMyInitialized(UIElement element, bool value)
{
element.SetValue(MyInitializedProperty, value);
}

public static bool GetMyInitialized(UIElement element)
{
return (bool) element.GetValue(MyInitializedProperty);
}

public static readonly DependencyProperty MyInitializedProperty = DependencyProperty.RegisterAttached("MyInitialized", typeof (bool), typeof (App), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits, OnMyInitializedChanged));

private static void OnMyInitializedChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs ev)
{
if ((bool)ev.NewValue)
{
// registering instance handler unbreaks class handlers
if (dpo is FrameworkElement element)
element.Loaded += _EmptyRoutedEventHandler;
if (dpo is FrameworkContentElement contentElement)
contentElement.Loaded += _EmptyRoutedEventHandler;
} else
{
throw new ArgumentException("Cannot set to false", ev.Property.Name);
}
//Console.WriteLine("MyInitialized {0} {1}=>{2}", dpo, ev.OldValue, ev.NewValue);
}

private static readonly RoutedEventHandler _EmptyRoutedEventHandler = delegate { };

#endregion
}

XAML

<Window x:Class="WpfApp3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp3"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
d:DataContext="{d:DesignInstance local:MainWindow}">
<Grid>
<Border Background="Green" VerticalAlignment="Center" HorizontalAlignment="Center" Width="30" Height="20">
<TextBlock Background="Orange" Text="hello"></TextBlock>
</Border>
</Grid>
</Window>

示例 控制台 输出:

2018-07-31T14:20:52.6052225+02:00: WpfApp3.MainWindow loaded
2018-07-31T14:20:52.6112064+02:00: System.Windows.Controls.Border loaded
2018-07-31T14:20:52.6132008+02:00: System.Windows.Documents.AdornerDecorator loaded
2018-07-31T14:20:52.6141984+02:00: System.Windows.Controls.ContentPresenter loaded
2018-07-31T14:20:52.6141984+02:00: System.Windows.Controls.Grid loaded
2018-07-31T14:20:52.6151966+02:00: System.Windows.Controls.Border loaded
2018-07-31T14:20:52.6161935+02:00: System.Windows.Controls.TextBlock loaded
2018-07-31T14:20:52.6161935+02:00: System.Windows.Documents.AdornerLayer loaded

关于c# - 如何分配全局初始化事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51520406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com