WPF后台线程刷新UI

后台线程刷新前台界面

本篇介绍利用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介绍

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值