C# 在WPF窗口中更新文本框内容

C# 在WPF窗口中更新文本框内容,c#,wpf,C#,Wpf,我有一个WPF项目,在其中我正在执行一个安装 要显示安装进度,该窗口包含一个文本框,我想使用该文本框进行更新,例如: LogDisplay.AppendText("Initialising installation..." + "\r\n"); 这有点 问题是文本框的内容只有在安装完成后才会显示 我现在尝试了几种解决方案,例如: /* LogDisplay.Update; this.Update(); this.UpdateContent(); */ 但这些都对我有用 XAML代码是: &l

我有一个WPF项目,在其中我正在执行一个安装

要显示安装进度,该窗口包含一个文本框,我想使用该文本框进行更新,例如:

LogDisplay.AppendText("Initialising installation..." + "\r\n");
这有点

问题是文本框的内容只有在安装完成后才会显示

我现在尝试了几种解决方案,例如:

/*
LogDisplay.Update;
this.Update();
this.UpdateContent();
*/
但这些都对我有用

XAML代码是:

<Window x:Class="Steam_Server_Installer.UI.ServerInstallation"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Steam_Server_Installer.UI"
        mc:Ignorable="d"
        Title="" Height="600" Width="900"
        WindowStartupLocation="CenterScreen">

    <Grid>
        <TextBox x:Name="LogDisplay" HorizontalAlignment="Left" VerticalAlignment="Top" Height="470" Width="650" Margin="30,90,0,0" IsReadOnly="True"/>
        <Button x:Name="cancel_button" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,530,115,0" Width="70" Content="Cancel" FontSize="16" Click="cancel_button_Click"/>
        <Button x:Name="finish_start_button" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,530,25,0" Width="70" Content="Finish" FontSize="16" Click="finish_start_button_Click" IsEnabled="False"/>
    </Grid> 
</Window>


如果有人能告诉我一个有效的解决方案,或者给我指出另一个已经讨论过这个问题的问题,我们将不胜感激。

尝试使用TextBlock而不是像这样的TextBox

<TextBlock x:Name="LogDisplay" HorizontalAlignment="Left" VerticalAlignment="Top" Height="470" Width="650" Margin="30,90,0,0" />
在XAML中,使用:

<TextBlock Text="{Binding LogText, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" VerticalAlignment="Top" Height="470" Width="650" Margin="30,90,0,0" />

尝试使用TextBlock而不是像这样的TextBox

<TextBlock x:Name="LogDisplay" HorizontalAlignment="Left" VerticalAlignment="Top" Height="470" Width="650" Margin="30,90,0,0" />
在XAML中,使用:

<TextBlock Text="{Binding LogText, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" VerticalAlignment="Top" Height="470" Width="650" Margin="30,90,0,0" />

在UI线程中使用长任务时,UI线程不会空闲以更新其他内容控件。 您必须使用CreateOtherThread和HandleTasktoThread,然后使用UI线程更新UI控件

1.Create Thread or Task
2.Work task ...
3.Update the UI thread with Application.Current.Dispatcher.Invoke(() => { TextBox.Text = "text"; });
4.Finish

在UI线程中使用长任务时,UI线程不会空闲以更新其他内容控件。 您必须使用CreateOtherThread和HandleTasktoThread,然后使用UI线程更新UI控件

1.Create Thread or Task
2.Work task ...
3.Update the UI thread with Application.Current.Dispatcher.Invoke(() => { TextBox.Text = "text"; });
4.Finish

谢谢@rene,我会在再次询问时记住这一点……听起来你是在UI线程中运行安装,这就是为什么你的UI在流程完成后第一次更新的原因。请尝试在后台线程中运行它,或者在finish\u start\u button\u Click事件处理程序中使用async/await(/dispatcher)-概念。谢谢@rene,我会在再次询问时记住它……听起来您是在UI线程中运行安装,这就是为什么在流程完成时会首先更新UI的原因。请尝试在后台线程中运行它,或者在finish\u start\u button\u Click事件处理程序中使用async/await(/dispatcher)-概念。感谢您的快速回答,但由于我以前从未使用过类似的方法,您能解释一下构造函数的含义吗(它只是我向TextBlock添加文本的代码部分吗?)现在我该如何向LogOutput添加文本呢@Ali BaigI已经更新了我上面的答案并添加了构造函数代码,请查看。@XaverXor正如您所知,类中的构造函数是基于类实例化对象时执行的。在上面的示例中,DataContext属性是在实例化时设置的。再次感谢,但现在我得到了错误
“ServerInstallation”enthält keine Definition für“LogText”,以及es konnte keine LogText Erweiterungsmethode gefunden werden,die ein erstes参数vom Typ“ServerInstallation”akzeptiert(möglicherweise fehlt eine使用Direktive order ein assemblymverweis)。
@Ali BaigIt如果你能用英语发布它会很有帮助!谢谢你的快速回答,但是因为我以前从未使用过类似的东西,你能解释一下构造函数的意思吗(这只是我在代码中向文本块添加文本的部分吗?)现在我如何向LogOutput添加文本?..@Ali BaigI已经更新了上面的答案并添加了构造函数代码,请检查。@XaverXor正如您所知,类中的构造函数是基于类实例化对象时执行的。在上面的示例中,DataContext属性在实例化时设置。再次感谢,但现在我得到了错误
“服务器安装”enthält keine定义für“LogText”,以及es konnte keine LogText Erweiterungsmethode gefunden werden,die ein erstes参数vom Typ“服务器安装”akzeptiert(möglicherweise fehlt eine使用Direktive order ein Assemblyverweis).
@Ali BaigIt如果你能用英语发布,将会很有帮助!