对于WPF应用程序,在Visual Studio和Expression Blend中,自定义的窗体均继承System.Windows.Window类。用户通过窗口与 Windows Presentation Foundation (WPF) 独立应用程序进行交互。 窗口的主要用途是承载可视化数据并使用户可以与数据进行交互的内容。独立 WPF 应用程序使用 Window 类来提供它们自己的窗口。在 WPF 中,可以使用代码或 XAML 标记来实现窗口的外观和行为。我们这里定义的窗体也由这两部分组成:
<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>
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(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();
}
}
}
和所有类一样,窗口也有生存期,在第一次实例化窗口时生存期开始,然后就可以显示、激活和停用窗口,直到最终关闭窗口。
为了证实上面的结论,我们用下面的代码进行测试:
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(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();
}
}
}
打开窗体的事件执行顺序为:如下图。
WPF窗体的详细的属性、方法、事件请参考MSDN,有很多的属性、方法、事件与Windows应用程序中 System.Windows.Forms.Form类颇为相似。