一、窗体类基本概念

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

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

<Window x:Class="WpfApp1.WindowThd"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="WindowThd" Height="300" Width="400">

    <Grid>

        <StackPanel>

            <Label x:Name="lblHello">欢迎你光临WPF的世界!</Label>

            <Button Name="btnThd" Click="btnThd_Click" >多线程同步测试</Button>

            <Button Name="btnAppBeginInvoke" Click="btnAppBeginInvoke_Click" >BeginInvoke 异步调用</Button>

        </StackPanel>

    </Grid>

</Window>

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

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading;

using System.Threading.Tasks;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Shapes;

using System.Windows.Threading;

namespace WpfApp1

{

    /// <summary>

    /// WindowThd.xaml 的交互逻辑

    /// </summary>

    public partial class WindowThd : Window

    {

        public WindowThd()

        {

            InitializeComponent();

    }

    private void ModifyUI()

    {

          // 模拟一些工作正在进行

        Thread.Sleep(TimeSpan.FromSeconds());

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

                    this.lblHello.Content = "欢迎你光临WPF的世界,Dispatche 异步方法!!"+ DateTime.Now.ToString();

                }));

        }).Start();

    }

    }

}

二、窗体的生命周期

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

1、显示窗体

  • 构造函数
  • Show()、ShowDialog()方法:Show()方法显示非模态窗口,这意味着应用程序所运行的模式允许用户在同一个应用程序中激活其他窗口。ShowDialog()方法显示模态窗口,这个基本和WinForm类似
  • 当初始化窗口时,将引发 SourceInitialized 事件并显示窗口。

2、窗体的激活

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

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

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

3、关闭窗体

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

  • Close()方法:关闭窗体,并释放窗体的资源
  • Closing事件、Closed事件:关闭时、关闭后引发的事件,通常在Closing事件中提示用户是否退出等信息。

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

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

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading;

using System.Threading.Tasks;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Shapes;

using System.Windows.Threading;

namespace WpfApp1

{

    /// <summary>

    /// WindowThd.xaml 的交互逻辑

    /// </summary>

    public partial class WindowThd : Window

    {

        public WindowThd()

        {

            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("Deactivated!");

        }

        void WindowThd_ContentRendered(object sender, EventArgs e)

        {

            Console.WriteLine("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());

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

                    this.lblHello.Content = "欢迎你光临WPF的世界,Dispatche 异步方法!!"+ DateTime.Now.ToString();

                }));

        }).Start();

    }

    }

}

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

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

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

WPF入门教程系列五——Window 介绍的更多相关文章

  1. WPF入门教程系列三——Application介绍(续)

    接上文WPF入门教程系列二——Application介绍,我们继续来学习Application 三.WPF应用程序的关闭 WPF应用程序的关闭只有在应用程序的 Shutdown 方法被调用时,应用程序 ...

  2. WPF入门教程系列二——Application介绍

    一.Application介绍 WPF和WinForm 很相似, WPF与WinForm一样有一个 Application对象来进行一些全局的行为和操作,并且每个 Domain (应用程序域)中仅且只 ...

  3. WPF入门教程系列六——布局介绍与Canvas(一)

    从这篇文章开始是对WPF中的界面如何布局做一个较简单的介绍,大家都知道:UI是做好一个软件很重要的因素,如果没有一个漂亮的UI,功能做的再好也无法吸引很多用户使用,而且没有漂亮的界面,那么普通用户会感 ...

  4. WPF入门教程系列四——Dispatcher介绍

    一.Dispatcher介绍 微软在WPF引入了Dispatcher,那么这个Dispatcher的主要作用是什么呢? 不管是WinForm应用程序还是WPF应用程序,实际上都是一个进程,一个进程可以 ...

  5. WPF入门教程系列二十三——DataGrid示例(三)

    DataGrid的选择模式 默认情况下,DataGrid 的选择模式为“全行选择”,并且可以同时选择多行(如下图所示),我们可以通过SelectionMode 和SelectionUnit 属性来修改 ...

  6. WPF入门教程系列(一) 创建你的第一个WPF项目

    WPF入门教程系列(一) 创建你的第一个WPF项目 WPF基础知识 快速学习绝不是从零学起的,良好的基础是快速入手的关键,下面先为大家摞列以下自己总结的学习WPF的几点基础知识: 1) C#基础语法知 ...

  7. WPF入门教程系列(二) 深入剖析WPF Binding的使用方法

    WPF入门教程系列(二) 深入剖析WPF Binding的使用方法 同一个对象(特指System.Windows.DependencyObject的子类)的同一种属性(特指DependencyProp ...

  8. WPF入门教程系列一

    WPF入门教程 一.  前言  公司项目基于WPF开发,最近项目上线有点空闲时间写一篇基于wpf的基础教材,WPF也是近期才接触,学习WPF也是在网上查资料与微软的MSDN进行学习,写本博客的目为了温 ...

  9. WPF入门教程系列三

    WPF之Binding的使用(一) 一.  前言 初学WPF经常被Binding搞得苦不堪言,Binding的重用性就不做介绍了,在WPF应用程序开发中Binding是一个非常重要的部分.WPF也是近 ...

随机推荐

  1. SQL Server 查出未提交事务(长事务)SQL

    ),value )); INSERT INTO @tab EXEC('DBCC OPENTRAN WITH TABLERESULTS'); SELECT name,CAST(value AS DATE ...

  2. 42. Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  3. (转)CentOs上配置samba服务

    前 言 在我们使用 Windows 作为客户机的时候,通常有文件.打印共享的需求.作为Windows 网络功能之一,通常可以在 Windows 客户机之间通过 Windows Network 固有的功 ...

  4. 用sql语句清除日志

    DUMP TRANSACTION [数据库] WITH NO_LOGBACKUP LOG [数据库] WITH NO_LOGDBCC SHRINKDATABASE([数据库])

  5. 关于webapp的一点思考

    早上上班路上碰到发传单推广app的小MM被上司训斥,忽然想起一个问题,现在的nativeapp推广成本到底多高?能不能用浏览器访问app的方式降低这个成本. 现在很多app采用壳+web内容的方式,但 ...

  6. 前端学习之touch.js与swiper学习

    Touch.js是移动设备上的手势识别与事件库,改框架基于原生js,操作简单,主要分drag,swipe,rotate,scale,tab,hold,touch操作. swiper是一个移动端触摸滑动 ...

  7. smartupload 上传与下载(转载)

    前台: <form action="uploadimage.jsp" method="post" enctype="multipart/form ...

  8. 第57讲:Scala中Dependency Injection实战详解

    本讲我们来学习下依赖注入.让我们从代码出发: package scala.learn trait Logger {def log (msg:String)}trait Auth {  auth:Log ...

  9. Java学习笔记二——标识符和关键字

    标识符 定义 标识符的定义:对各种变量.方法和类等要素命名时使用的字符序列成为标识符. 简单地说,就是凡是自己可以起名字的地方都叫标识符,都要遵守标识符的规则. 命名规则 标识符只能由字母.下划线&q ...

  10. MFC学习随笔(1)

    最近在学习用MFC编写一个简单的界面.其实MFC并不是十分复杂的,经过一段时间的熟悉就可以实现许多基础功能.但是在编写的过程中,经常会遇到林林总总的bug,让人摸不到头脑.今天记录一个过去没有注意过的 ...