Loaded event of a WPF user control fires more than once

前端 未结 3 2010
忘掉有多难
忘掉有多难 2020-12-05 01:54

To implement a tab-based environment in WPF we need to convert our forms to user controls, however when doing this, the Loaded event of the use

3条回答
  •  甜味超标
    2020-12-05 02:42

    Your routed event handler can (and should) remove itself from the Loaded hook as the first thing it does.

    public class MyClass : Window
    {
        public MyClass()
        {
            Loaded += MyLoadedRoutedEventHandler;
        }
    
        void MyLoadedRoutedEventHandler(Object sender, RoutedEventArgs e)
        {
            Loaded -= MyLoadedRoutedEventHandler;
            /// ...
        }
    };
    

提交回复
热议问题