您好,
会员登录 快速注册
退出 ( 条未读消息)
关于本站 意见反馈 首页

公告:小宅博客网可以开发票了,需要发票的,去群里找群主哈!!
全部文章分类
  • 人工智能 >

  • 编程语言 >

  • WPF系列 >

  • ASP.NET系列 >

  • Linux >

  • 数据库 >

  • 嵌入式 >

  • WEB技术 >

  • PLC系列 >

  • 微服务与框架 >

  • 小宅DIY >

  • 学习资料 >

OpenCv基础 ANN车牌识别 yolov5车牌识别 指针式仪表识别 ROS系列 YOLO Halcon Detectron2 昇腾AI ChatGPT在线体验 英伟达JETSON ChatGLM ChatTTS FunASR 地平线 ByteTrack 魔搭社区 LangChain
C C# C++ Python Java Go
WPF
ASP.NET小功能 GPS定位系统-MVC GPS定位系统-VUE ASP.NET WebRTC
Linux Linux内核 Shell MakeFile
MySql SqlServer Oracle
STM8 STM32 51单片机
VUE入门 HTML JavaScript CSS layui镜像网站 ElementUi中文官网 element-plus 图标
三菱 欧姆龙 西门子 施耐德 松下 台达
IOTSharp IOTGateway ABP FRAMEWORK Docker
亚克力音响 编程仙途:智驭万法
面试题与技巧 Python入门技能树 微软C#教程
首页 编程之美 工具下载 全国就业 流量地图 文心一言
WPF
内容介绍 1、新建WPF工程 2、Application介绍 3、Dispatcher介绍 4、Window 介绍 5、主要布局属性介绍 6、Grid网格布局 7、UniformGrid布局 8、DockPanel与ViewBox布局 9、Border与ViewBox布局 10、依赖属性(一) 11、依赖属性(二) 12、依赖属性(三) 13、依赖属性(四) 14、WPF中的数据绑定(一) 15、WPF中的数据绑定(二) 16、WPF中的数据绑定(三) 17、WPF中的数据绑定(四) 18、ListView示例(一) 19、ListView示例(二) 20、DataGrid示例(一) 21、DataGrid示例(二) 22、DataGrid示例(三) 23、引用FontAwesome矢量图 24、ListBox日志效果 25、Polygon绘制多边形 26、Ellipse绘制实心圆 27、数据模板DataTemplate 基于WPF的exe远程升级程序
3、Dispatcher介绍
5、主要布局属性介绍
激萌の小宅 博客园 WPF

文章作者:激萌の小宅

促销:¥0

价格:¥0

配送方式: 购买后立即生效(如购买异常,请联系站长)
付款之后一定要等待自动跳转结束,否则购买可能会失败
  • 0 天

    有效期

  • 0

    总销量

  • 0

    累计评价

Window 介绍 - (四)

一、窗体类基本概念

       对于WPF应用程序,在Visual Studio和Expression Blend中,自定义的窗体均继承System.Windows.Window类。用户通过窗口与 Windows Presentation Foundation (WPF) 独立应用程序进行交互。 窗口的主要用途是承载可视化数据并使用户可以与数据进行交互的内容。独立 WPF 应用程序使用 Window 类来提供它们自己的窗口。在 WPF 中,可以使用代码或 XAML 标记来实现窗口的外观和行为。我们这里定义的窗体也由这两部分组成:

1、 XAML文件(MainWindow.xaml),在这里面通常全部写UI的东西,包括窗口的外观,控件等。

2、窗口界面中的各种行为,则由后台代码文件(MainWindow.xaml.cs)决定。


二、窗体的生命周期

        和所有类一样,窗口也有生存期,在第一次实例化窗口时生存期开始,然后就可以显示、激活和停用窗口,直到最终关闭窗口。


1、显示窗体

      1)构造函数 

      2) Show()、ShowDialog()方法:Show()方法显示非模态窗口,这意味着应用程序所运行的模式允许用户在同一个应用程序中激活其他窗口。ShowDialog()方法显示模态窗口,这个基本和WinForm类似 

      3)当初始化窗口时,将引发 SourceInitialized 事件并显示窗口。


2、窗体的激活

       在首次打开一个窗口时,它便成为活动窗口(除非是在 ShowActivated 设置为 false 的情况下显示)。 活动窗口是当前正在捕获用户输入(例如,键击和鼠标单击)的窗口。 当窗口变为活动窗口时,它会引发 Activated 事件。

       当第一次打开窗口时,只有在引发了 Activated 事件之后,才会引发 Loaded 和 ContentRendered 事件。 记住这一点,在引发 ContentRendered 时,便可认为窗口已打开。

       窗口变为活动窗口之后,用户可以在同一个应用程序中激活其他窗口,还可以激活其他应用程序。 当这种情况出现时,当前的活动窗口将停用,并引发 Deactivated 事件。 同样,当用户选择当前停用的窗口时,该窗口会再次变成活动窗口并引发 Activated。 


3、关闭窗体

       当用户关闭窗口时,窗口的生命便开始走向终结。

       Close()方法:关闭窗体,并释放窗体的资源 

       Closing事件、Closed事件:关闭时、关闭后引发的事件,通常在Closing事件中提示用户是否退出等信息。 


4、窗体的生命周期。如下图。

为了证实上面的结论,我们用下面的代码进行测试:

using System;
using System.Threading;
using System.Windows;
using System.Windows.Threading;

namespace WpfApp
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.Activated += WindowThd_Activated;
            this.Closing += WindowThd_Closing;
            this.ContentRendered += WindowThd_ContentRendered;
            this.Deactivated += WindowThd_Deactivated;
            this.Loaded += WindowThd_Loaded;
            this.Closed += WindowThd_Closed;
            this.Unloaded += WindowThd_Unloaded;
            this.SourceInitialized += WindowThd_SourceInitialized;

            InitializeComponent();
        }

        void WindowThd_SourceInitialized(object sender, EventArgs e)
        {
            Console.WriteLine("1---------------->SourceInitialized!");
        }

        void WindowThd_Unloaded(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("---------------->Unloaded!");
        }

        void WindowThd_Closed(object sender, EventArgs e)
        {
            Console.WriteLine("---------------->_Closed!");
        }

        void WindowThd_Loaded(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("3---------------->Loaded!");
        }

        void WindowThd_Deactivated(object sender, EventArgs e)
        {
            Console.WriteLine("5---------------->Deactivated!");
        }

        void WindowThd_ContentRendered(object sender, EventArgs e)
        {
            Console.WriteLine("4---------------->ContentRendered!");
        }

        void WindowThd_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Console.WriteLine("---------------->Closing!");
        }

        void WindowThd_Activated(object sender, EventArgs e)
        {
            Console.WriteLine("2---------------->Activated!");
        }

        private void ModifyUI()
        {
            // 模拟一些工作正在进行
            Thread.Sleep(TimeSpan.FromSeconds(2));
            //lblHello.Content = "欢迎你光临WPF的世界,Dispatcher";
            this.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate ()
            {
                lblHello.Content = "欢迎你光临WPF的世界,Dispatche  同步方法 !!";
            });
        }

        private void btnThd_Click(object sender, RoutedEventArgs e)
        {
            Thread thread = new Thread(ModifyUI);
            thread.Start();
        }

        private void btnAppBeginInvoke_Click(object sender, RoutedEventArgs e)
        {
            new Thread(() =>
            {
                Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                    new Action(() =>
                    {
                        Thread.Sleep(TimeSpan.FromSeconds(2));
                        this.lblHello.Content = "欢迎你光临WPF的世界,Dispatche 异步方法!!" + DateTime.Now.ToString();
                    }));
            }).Start();
        }
    }
}

执行结果如下:

启动了...
1---------------->SourceInitialized!
2---------------->Activated!
3---------------->Loaded!
4---------------->ContentRendered!
---------------->Closing!
5---------------->Deactivated!
---------------->_Closed!


打开窗体的事件执行顺序为:如下图。


关闭窗体的事件执行顺序为:如下图。


WPF窗体的详细的属性、方法、事件请参考MSDN,有很多的属性、方法、事件与Windows应用程序中 System.Windows.Forms.Form类颇为相似。 


MSDN官网

MSDN官网WPF资料

3、Dispatcher介绍
5、主要布局属性介绍

友情链接: CSDN激萌の小宅 95知识库 自考题库 罗分明个人网络博客 精益编程leanboot

小宅博客  www.bilibili996.com All Rights Reserved. 备案号: 闽ICP备2024034575号

网站经营许可证  福建省福州市 Copyright©2021-2025 版权所有

小宅博客
首页 智能家居 地图定位
公告:小宅博客网可以开发票了,需要发票的,去群里找群主哈!!

文章作者:激萌の小宅

促销:¥0

价格:¥0

配送方式: 购买后立即生效(如购买异常,请联系站长)
付款之后一定要等待自动跳转结束,否则购买可能会失败
  • 0 天

    有效期

  • 0

    总销量

  • 0

    累计评价

Window 介绍 - (四)

一、窗体类基本概念

       对于WPF应用程序,在Visual Studio和Expression Blend中,自定义的窗体均继承System.Windows.Window类。用户通过窗口与 Windows Presentation Foundation (WPF) 独立应用程序进行交互。 窗口的主要用途是承载可视化数据并使用户可以与数据进行交互的内容。独立 WPF 应用程序使用 Window 类来提供它们自己的窗口。在 WPF 中,可以使用代码或 XAML 标记来实现窗口的外观和行为。我们这里定义的窗体也由这两部分组成:

1、 XAML文件(MainWindow.xaml),在这里面通常全部写UI的东西,包括窗口的外观,控件等。

2、窗口界面中的各种行为,则由后台代码文件(MainWindow.xaml.cs)决定。


二、窗体的生命周期

        和所有类一样,窗口也有生存期,在第一次实例化窗口时生存期开始,然后就可以显示、激活和停用窗口,直到最终关闭窗口。


1、显示窗体

      1)构造函数 

      2) Show()、ShowDialog()方法:Show()方法显示非模态窗口,这意味着应用程序所运行的模式允许用户在同一个应用程序中激活其他窗口。ShowDialog()方法显示模态窗口,这个基本和WinForm类似 

      3)当初始化窗口时,将引发 SourceInitialized 事件并显示窗口。


2、窗体的激活

       在首次打开一个窗口时,它便成为活动窗口(除非是在 ShowActivated 设置为 false 的情况下显示)。 活动窗口是当前正在捕获用户输入(例如,键击和鼠标单击)的窗口。 当窗口变为活动窗口时,它会引发 Activated 事件。

       当第一次打开窗口时,只有在引发了 Activated 事件之后,才会引发 Loaded 和 ContentRendered 事件。 记住这一点,在引发 ContentRendered 时,便可认为窗口已打开。

       窗口变为活动窗口之后,用户可以在同一个应用程序中激活其他窗口,还可以激活其他应用程序。 当这种情况出现时,当前的活动窗口将停用,并引发 Deactivated 事件。 同样,当用户选择当前停用的窗口时,该窗口会再次变成活动窗口并引发 Activated。 


3、关闭窗体

       当用户关闭窗口时,窗口的生命便开始走向终结。

       Close()方法:关闭窗体,并释放窗体的资源 

       Closing事件、Closed事件:关闭时、关闭后引发的事件,通常在Closing事件中提示用户是否退出等信息。 


4、窗体的生命周期。如下图。

为了证实上面的结论,我们用下面的代码进行测试:

using System;
using System.Threading;
using System.Windows;
using System.Windows.Threading;

namespace WpfApp
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.Activated += WindowThd_Activated;
            this.Closing += WindowThd_Closing;
            this.ContentRendered += WindowThd_ContentRendered;
            this.Deactivated += WindowThd_Deactivated;
            this.Loaded += WindowThd_Loaded;
            this.Closed += WindowThd_Closed;
            this.Unloaded += WindowThd_Unloaded;
            this.SourceInitialized += WindowThd_SourceInitialized;

            InitializeComponent();
        }

        void WindowThd_SourceInitialized(object sender, EventArgs e)
        {
            Console.WriteLine("1---------------->SourceInitialized!");
        }

        void WindowThd_Unloaded(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("---------------->Unloaded!");
        }

        void WindowThd_Closed(object sender, EventArgs e)
        {
            Console.WriteLine("---------------->_Closed!");
        }

        void WindowThd_Loaded(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("3---------------->Loaded!");
        }

        void WindowThd_Deactivated(object sender, EventArgs e)
        {
            Console.WriteLine("5---------------->Deactivated!");
        }

        void WindowThd_ContentRendered(object sender, EventArgs e)
        {
            Console.WriteLine("4---------------->ContentRendered!");
        }

        void WindowThd_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Console.WriteLine("---------------->Closing!");
        }

        void WindowThd_Activated(object sender, EventArgs e)
        {
            Console.WriteLine("2---------------->Activated!");
        }

        private void ModifyUI()
        {
            // 模拟一些工作正在进行
            Thread.Sleep(TimeSpan.FromSeconds(2));
            //lblHello.Content = "欢迎你光临WPF的世界,Dispatcher";
            this.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate ()
            {
                lblHello.Content = "欢迎你光临WPF的世界,Dispatche  同步方法 !!";
            });
        }

        private void btnThd_Click(object sender, RoutedEventArgs e)
        {
            Thread thread = new Thread(ModifyUI);
            thread.Start();
        }

        private void btnAppBeginInvoke_Click(object sender, RoutedEventArgs e)
        {
            new Thread(() =>
            {
                Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                    new Action(() =>
                    {
                        Thread.Sleep(TimeSpan.FromSeconds(2));
                        this.lblHello.Content = "欢迎你光临WPF的世界,Dispatche 异步方法!!" + DateTime.Now.ToString();
                    }));
            }).Start();
        }
    }
}

执行结果如下:

启动了...
1---------------->SourceInitialized!
2---------------->Activated!
3---------------->Loaded!
4---------------->ContentRendered!
---------------->Closing!
5---------------->Deactivated!
---------------->_Closed!


打开窗体的事件执行顺序为:如下图。


关闭窗体的事件执行顺序为:如下图。


WPF窗体的详细的属性、方法、事件请参考MSDN,有很多的属性、方法、事件与Windows应用程序中 System.Windows.Forms.Form类颇为相似。 


MSDN官网

MSDN官网WPF资料