C#WPF-如何从另一个类/线程简单地更新UI [英] C# WPF - How to simply update UI from another class/thread

查看:558
本文介绍了C#WPF-如何从另一个类/线程简单地更新UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到针对此问题的简单解决方案.那就是我问的原因.

I couldn't find a simple solution to this problem. That's why I'm asking.

我有这样的WPF 窗口:

<Window x:Class="WPF_Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Width="640" Height="480">

    <Button Name="xaml_button" Content="A Text."/>
</Window>

和MainWindow :

And a MainWindow class:

using System.Windows;
using System.Threading;

namespace WPF_Test
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            xaml_button.Content = "Text changed on start.";
        }
    }

    private void xaml_button_Click()
    {
        Threading.t1.Start();
        UIControl.ChangeButtonName("Updated from another CLASS.");
    }
}

按钮的Content属性成功进行了更改.但是我要做的是在另一个线程中更改属性.我试过的是这个:

The button's Content property successfully changed itself. But what I want to do, is to change the property in another class or thread. What I tried is this:

class UIControl
{
    public static void ChangeButtonName(string text)
    {
        var window = new MainWindow();
        window.xaml_button.Content = text;
    }
}

它显然不起作用,因为public MainWindow()Content属性更改回原始属性,并带来了一些问题.

It obviously doesn't work, because the public MainWindow() changes the Content property back to the original one, and brings some issues along with it.

我也想在多线程时使用它.我简单的线程类看起来像这样:

Also I want to use this while multithreading. My simple threading class looks like this:

class Threading
{
    public static Thread t1 = new Thread(t1_data);

    static void t1_data()
    {
        Thread.Sleep(2000);
        UIControl.ChangeButtonName("Updated from another THREAD.");
    }
}

推荐答案

为此,建议您声明一个 static 变量,该变量包含您喜欢的UI控件,在这种情况下, Button.还要在开头添加using System.Windows.Controls;.因此,您的代码应如下所示:

To do this, I'd recommend declaring a static variable that holds a UI control of your likings, in this case, a Button. Also add using System.Windows.Controls; at the beginning. So your code should look like this:

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

namespace WPF_Test
{
    public partial class MainWindow : Window
    {
        public static Button xamlStaticButton;

        public MainWindow()
        {
            InitializeComponent();
            xamlStaticButton = xaml_button;
            xamlStaticButton.Content = "Text changed on start";
        }

        private void xaml_button_Click(object sender, RoutedEventArgs e)
        {
            Threading.t1.Start();
            UIControl.ChangeButtonName("Updated from another CLASS.");
        }
    }
}

我所做的几乎就是为按钮创建占位符,然后在开始时分配.

So pretty much what I did was to make a placeholder for the button, then assigning it on start.

class UIControl : MainWindow
{
    public static void ChangeButtonName(string text)
    {
        App.Current.Dispatcher.Invoke(delegate {
            xamlStaticButton.Content = text;
        });
    }
}

现在,为了方便起见,我继承了类到UIControl类.为了使此功能与多线程一起使用,我添加了App.Current.Dispatcher.Invoke(delegate { /*your UI code you want to execute*/});.这将确保,即使您位于另一个线程上,您的用户界面也会更新.

Now I inherited the MainWindow class to the UIControl class for the sake of convenience. Also to make this work with multithreading, I added the App.Current.Dispatcher.Invoke(delegate { /*your UI code you want to execute*/});. This will make sure your UI will update even if you are on another thread.

这篇关于C#WPF-如何从另一个类/线程简单地更新UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆