后台线程刷新前台界面
本篇介绍利用Dispatcher实现从一个非 UI 线程中更新一个由UI线程创建的对象
1.Dispatcher简介
微软在WPF中引入了Dispatcher,它是用于管理线程工作项队列,类似于Win32中的消息队列,所有WPF应用程序启动时都会加载两个重要的线程:一个用于呈现用户界面,另一个用于管理用户界面。呈现线程是一个在后台运行的隐藏线程,通常我们面对的线程是UI线程。WPF要求将其大多数对象与UI线程进行关联,意味着要使用一个WPF对象,只能在创建它的线程上使用,在其它线程上使用时会引发异常:"System.InvalidOperationException"类型的未经处理的异常在WindowsBase.dll中发生 其他信息:调用线程无法访问此对象,因为另一个线程拥有该对象。那么如何实现从一个非UI线程中更新一个由UI线程创建的对象呢?
2. Dispatcher使用
功能:点击修改文本按钮,10S后文本框显示内容修改为OK。
前端PageHelp.xaml代码如下:
<Page x:Class="wpfbase.PageHelp"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:wpfbase"
mc:Ignorable="d"
d:DesignHeight="325" d:DesignWidth="525"
Title="PageHelp">
<Page.Resources>
<!-- 定义一个key=FileButtonStyle的Button样式 -->
<Style TargetType="Button" x:Key="FileButtonStyle">
<Style.Setters>
<Setter Property="FontFamily" Value="Times New Roman" />
<Setter Property="FontSize" Value="12" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Background" Value="#ffffff" />
</Style.Setters>
<!-- FileButtonStyle样式触发器 -->
<Style.Triggers>
<!-- 获得焦点时触发 -->
<Trigger Property="Control.IsFocused" Value="True">
<Setter Property="Control.Foreground" Value="Red" />
</Trigger>
<!-- 鼠标移过时触发 -->
<Trigger Property="Control.IsMouseOver" Value="True">
<Setter Property="Control.Foreground" Value="Yellow" />
<Setter Property="Control.FontWeight" Value="Bold" />
</Trigger>
<!-- 按钮按下时触发 -->
<Trigger Property="Button.IsPressed" Value="True">
<Setter Property="Control.Foreground" Value="Blue" />
</Trigger>
</Style.Triggers>
</Style>
</Page.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Content="返回" Click="GoBack"/>
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Grid.Row="1">
<StackPanel Orientation="Vertical">
<TextBox Name="textbox" AcceptsReturn="True">这里是帮助文档!</TextBox>
<Button Content="提交1" Style="{StaticResource FileButtonStyle}" Click="Test1"/>
<Button Content="提交2" Style="{StaticResource FileButtonStyle}" Click="Test2"/>
<Button Content="修改文本" Style="{StaticResource FileButtonStyle}" Click="ChangeText"/>
</StackPanel>
</ScrollViewer>
</Grid>
</Page>
后台PageHelp.xaml.cs错误的更新方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
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.Navigation;
using System.Windows.Shapes;
namespace wpfbase
{
public partial class PageHelp : Page
{
public PageHelp()
{
InitializeComponent();
}
private void Test1(object sender, RoutedEventArgs e) {
Console.WriteLine("Test1");
}
private void Test2(object sender, RoutedEventArgs e) {
Console.WriteLine("Test2");
}
private void ChangeText(object sender, RoutedEventArgs e) {
Thread thread = new Thread(fun);
thread.Start();
}
private void GoBack(object sender, RoutedEventArgs e) {
if(NavigationService.GetNavigationService(this).CanGoBack)
NavigationService.GetNavigationService(this).GoBack();
}
private void fun() {
Thread.Sleep(10);
textbox.Text = "OK";
return;
}
}
}
运行后点击修改文本按钮会出现下图所示的报错:
后台PageHelp.xaml.cs正确的更新方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
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.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace wpfbase
{
public partial class PageHelp : Page
{
public PageHelp()
{
InitializeComponent();
}
private void Test1(object sender, RoutedEventArgs e) {
Console.WriteLine("Test1");
}
private void Test2(object sender, RoutedEventArgs e) {
Console.WriteLine("Test2");
}
private void ChangeText(object sender, RoutedEventArgs e) {
Thread thread = new Thread(fun);
thread.Start();
}
private void GoBack(object sender, RoutedEventArgs e) {
if(NavigationService.GetNavigationService(this).CanGoBack)
NavigationService.GetNavigationService(this).GoBack();
}
private void fun() {
Thread.Sleep(10);
this.Dispatcher.Invoke(DispatcherPriority.Normal,
(ThreadStart)delegate() {
textbox.Text = "OK";
});
}
}
}
只需改动函数fun中的内容
参考: Dispatcher介绍