Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改文本单击可阻止文本_C#_.net_Wpf_Wpf Controls_Textblock - Fatal编程技术网

C# 更改文本单击可阻止文本

C# 更改文本单击可阻止文本,c#,.net,wpf,wpf-controls,textblock,C#,.net,Wpf,Wpf Controls,Textblock,我有一个,里面有一个和一个,我正在尝试更改Mouseleftbutton down上的textblock文本,因为在这个click事件中有一些更长的操作,文本块的文本在这个操作完成之前不会更改 我还尝试了Dispatcher.BeginInvoke(),但没有成功 这是我的代码: 在线程上执行冗长的操作并使用 调度员 以相应地更新UI 示例代码: var opThread = new Thread(delegate() { //your lengthy operation tb

我有一个
,里面有一个
和一个
,我正在尝试更改Mouseleftbutton down上的textblock文本,因为在这个click事件中有一些更长的操作,文本块的文本在这个操作完成之前不会更改

我还尝试了
Dispatcher.BeginInvoke()
,但没有成功

这是我的代码:


在线程上执行冗长的操作并使用

调度员

以相应地更新UI

示例代码:

var opThread = new Thread(delegate()
{
    //your lengthy operation

    tbReadToMe.Dispatcher.Invoke(new Action(delegate
    {
        tbReadToMe.Text = "Pause";
    }));

    //your lengthy operation

    tbReadToMe.Dispatcher.Invoke(new Action(delegate
    {
        tbReadToMe.Text = "etc...";
    }));
});

opThread.Start();

在“冗长”操作之前,请使用
应用程序.DoEvents()

Dispatcher.BeginInvoke()没有帮助的原因是,即使它是异步运行的,它仍然在主/UI线程上运行。使用BackgroundWorkerThread类在后台线程上执行冗长的操作

我制作了一个小样本来演示:

Window1.xaml:

<Window x:Class="BackgroundWorkerExample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="663">
<Grid>
    <TextBox PreviewMouseDown="textBox1_PreviewMouseDown" Height="38" Margin="24,34,26,0" Name="textBox1" VerticalAlignment="Top" FontSize="24">
        The quick Velociraptor jumped over the torpid tapir.
    </TextBox>
</Grid>

您是否在单独的线程上调用冗长的操作?如果没有,它将在UI线程上运行,并且调用Dispatcher上的文本更改不会做任何事情,因为它正在等待冗长的操作完成。看到一些辅助操作将有所帮助。此外,您可能需要查看
BackgroundWorker
类及其
ProgressChanged
事件。没有,没有,没有成功…同样,我在Dispatcher.BeginInvoke中放入了冗长的操作部分…这是错误的,System.Windows.Forms.Application.DoEvents()不应用于此操作。长时间的操作不应在主线程上运行。它可能会工作,但应该避免这样做。这是编码器的选择!我不确定wpf中是否有
应用程序.DoEvents()
。非常抱歉!我的答案是用于您的工作。它比使用多线程要好!
var opThread = new Thread(delegate()
{
    //your lengthy operation

    tbReadToMe.Dispatcher.Invoke(new Action(delegate
    {
        tbReadToMe.Text = "Pause";
    }));

    //your lengthy operation

    tbReadToMe.Dispatcher.Invoke(new Action(delegate
    {
        tbReadToMe.Text = "etc...";
    }));
});

opThread.Start();
<Window x:Class="BackgroundWorkerExample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="663">
<Grid>
    <TextBox PreviewMouseDown="textBox1_PreviewMouseDown" Height="38" Margin="24,34,26,0" Name="textBox1" VerticalAlignment="Top" FontSize="24">
        The quick Velociraptor jumped over the torpid tapir.
    </TextBox>
</Grid>
using System.Windows;
using System.Windows.Input;
using System.ComponentModel;
using System.Threading;
using System.Windows.Media;
using System;

namespace BackgroundWorkerExample
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }


        void _asyncSpeakerThread_DoWork(object sender, DoWorkEventArgs e)
        {
            // Change color of text to Red to indicate Start of operation
            this.Dispatcher.BeginInvoke(new Action(() =>  { textBox1.Foreground = Brushes.Red; }));

            string text = e.Argument as string;
            //voice.Speak(text, SpeechVoiceSpeakFlags.SVSFDefault); // Lengthy operation 
            Thread.Sleep(1000); // Simulate lengthy operation

            // Change color of text to Black to indicate End of operation
            this.Dispatcher.BeginInvoke(new Action(() => { textBox1.Foreground = Brushes.Black; }));
        }

        private void textBox1_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            BackgroundWorker bw = new BackgroundWorker();

            bw.DoWork += new DoWorkEventHandler(_asyncSpeakerThread_DoWork);
            string workerArgument = textBox1.Text;
            bw.RunWorkerAsync(workerArgument);
        }
    }
}