首页 > 其他分享 >WPF初始化顺序

WPF初始化顺序

时间:2023-08-18 15:12:59浏览次数:26  
标签:初始化 顺序 EndInit 触发 void 事件 WriteLine Debug WPF

WPF的初始化的顺序

    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Activated(object sender, EventArgs e)
        {
            Debug.WriteLine("触发 WindowActivated 事件");
        }

        private void Window_Deactivated(object sender, EventArgs e)
        {
            Debug.WriteLine("触发 WindowDeactivated 事件");
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            base.OnClosing(e);

            Debug.WriteLine("触发 OnClosing 事件");
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            Debug.WriteLine("触发 OnClosed 事件");
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

            Debug.WriteLine("触发 OnInitialized 事件");
        }

        public override void BeginInit()
        {
            base.BeginInit();

            Debug.WriteLine("触发 BeginInit 事件");
        }

        public override void EndInit()
        {
            base.EndInit();

            Debug.WriteLine("触发 EndInit 事件");
        }

        protected override void OnActivated(EventArgs e)
        {
            base.OnActivated(e);

            Debug.WriteLine("触发 OnActivated 事件");
        }

        protected override void OnDeactivated(EventArgs e)
        {
            base.OnDeactivated(e);

            Debug.WriteLine("触发 OnDeactivated 事件");
        }

        protected override void OnContentChanged(object oldContent, object newContent)
        {
            base.OnContentChanged(oldContent, newContent);

            Debug.WriteLine("触发 OnContentChanged 事件");
        }

        protected override void OnContentRendered(EventArgs e)
        {
            base.OnContentRendered(e);

            Debug.WriteLine("触发 OnContentRendered 事件");
        }


    }

执行结果是

触发 BeginInit 事件
触发 OnContentChanged 事件
触发 OnInitialized 事件
触发 EndInit 事件
触发 BeginInit 事件
触发 EndInit 事件
触发 WindowActivated 事件
触发 OnActivated 事件
触发 OnContentRendered 事件
触发 OnClosing 事件
触发 WindowDeactivated 事件
触发 OnDeactivated 事件
触发 OnClosed 事件

但是为什么  初始化时候调用2次BeginInit/EndInit 2次呢?

构造函数里面调用InitializeComponent()时产生的BeginInit/EndInit,另外一次是在系统自动调用InitializeComponent()时产生的BeginInit/EndInit

构造函数里面调用InitializeComponent()时产生
触发 BeginInit 事件
触发 OnContentChanged 事件
触发 OnInitialized 事件
触发 EndInit 事件
//构造系统调用InitializeComponent()时产生
触发 BeginInit 事件
触发 EndInit 事件
//
触发 WindowActivated 事件
触发 OnActivated 事件
触发 OnContentRendered 事件
触发 OnClosing 事件
触发 WindowDeactivated 事件
触发 OnDeactivated 事件
触发 OnClosed 事件;

标签:初始化,顺序,EndInit,触发,void,事件,WriteLine,Debug,WPF
From: https://www.cnblogs.com/stweily/p/17640556.html

相关文章

  • WPF加载GIF的五种方式(Storyboard / WpfAnimatedGif / ImageAnimator / PictureBox / M
    部分内容参考博文WPF如何显示gif一、使用Storyboard效果:  (1)页面xaml:<Windowx:Class="PlayGifDemo.StoryboardWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2......
  • wpf 自定义轮播图组件
      轮播图组件代码:[Localizability(LocalizationCategory.None,Readability=Readability.Unreadable)][TemplatePart(Name="Part_Grid",Type=typeof(Grid))][TemplatePart(Name="Part_OldContentControl",Type=typeof(ContentControl))][Template......
  • WPF ListBox 控件绑定 Binding
     当我们需要用到循环的列表内容,并且模板化程度高的时候,建议使用 ListBox 来做绑定。XAML:<Window.DataContext><local:VMTempTest/></Window.DataContext><StackPanelMargin="10,0,0,50"Orientation="Vertical"><TextBlockText="ListBo......
  • WebView2在WPF中的应用
    开发环境运行环境:.Net6开发环境:VisualStudio202217.1.3框架语言:WPF安装WebView2通过PackageManager控制台安装Install-PackageMicrosoft.Web.WebView2通过Nuget包管理器安装在窗体中添加名字空间:xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;asse......
  • 为WPF框架Prism注册Nlog日志服务
    这篇文章介绍了为WPF框架Prism注册Nlog日志服务的方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 无论是Nlog还是Serilog,它们都提供了如何快速在各类应用程序当中的快速使用方法。尽管,你现在无论是在WPF或者ASP.NETCore当中,......
  • WPF的5种绑定模式(Mode)
    一:WPF的5种绑定模式(Mode)WPF的绑定模式(mode)是枚举的枚举值共有5个1:OneWay(源变就更新目标属性)2:TwoWay(源变就更新目标并且目标变就更新源)3:OneTime(只根据源来设置目标,以后都不会变)4:OneWayToSource(与OneWay相反)5:Default(可以单向或双向,是靠被值定的源或目标是否有get或set来指定的)所......
  • WPF 设置第二次打开程序直接弹出第一次打开的程序
    激活已经打开窗口函数[DllImport("user32.dll")]privatestaticexternboolSetForegroundWindow(IntPtrhWnd);[DllImport("user32.dll")]privatestaticexternboolShowWindowAsync(IntPtrhWnd,intnCmdShow);[DllImport("user32.dll")]privatest......
  • c# - 如何在圆角 WPF 窗体中创建圆角矩形?
    我正在WPF中创建一个应用程序,我想要圆角。收到。现在窗体是无边框的,我正在尝试创建一个圆角矩形并将其放在顶部,使其看起来像Windows应用程序的顶部栏。我做不到。这是我的代码:<BorderCornerRadius="50,0,50,0"BorderBrush="Black"BorderThickness="2"Background="......
  • WPF利用依赖属性和命令编写自定义控件
    以实例讲解(大部分讲解在代码中)1,新建一个WPF项目,添加一个用户控件之后在用户控件里面添加几个控件用作测试,12345678910111213141516171819<UserControlx:Class="SelfControlDenpendy.SelfControl"             xmlns="http://schem......
  • WPF使用WPFMediaKit/AForge调用摄像头示例 .net core 8.0 也支持
    调用摄像头使我们经常会用到的一个功能,可以根据摄像头捕捉到的图像进行分析处理来做很多的东西,比如电子档案、图像识别、AI分析等等。本示例中主要介绍Nuget最常用的两个调用摄像头的轮子WPFMediaKit、AForge环境:VS2019,.NetFramework472WPF  (.netcore8.0 也支持)WPFMe......