Как назначить глобальное инициализированное событие? - программирование
Подтвердить что ты не робот

Как назначить глобальное инициализированное событие?

У меня есть этот код в моем 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 выстрелил за эту метку, даже несмотря на то, что мое нормальное загруженное событие (что я сделал для ярлыка) было пустым. Я также пробовал:

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

Но они не срабатывают, если я не добавлю еще одно пустое загруженное событие для этих элементов управления.

Моя цель - создать своего рода журнал времени для каждого элемента управления, который будет инициализирован.

Как я могу достичь этого без добавления загруженных событий на каждый отдельный элемент управления, который у меня есть?

(У меня есть много)

4b9b3361

Ответ 1

Вот ты где!

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>

Вывод пробной Console:

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