在WPF窗口中更新TextBox内容

时间:2017-01-28 16:25:14

标签: c# wpf

我正在进行WPF项目,我正在进行安装......

要显示安装进度,该窗口包含一个TextBox,我想使用它来更新,例如:

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

这有点......

问题是 TextBox 的内容仅在安装完成后才会显示。

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

/*
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>

如果有人可以告诉我一个有效的解决方案,或者指出我已经讨论过这个问题的其他问题,我们将非常感激。

2 个答案:

答案 0 :(得分:1)

在UI线程中使用长任务时,UI线程不会空闲以更新其他内容控件。 您必须使用create other thread和handle task来线程,然后使用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

答案 1 :(得分:0)

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

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

最好使用绑定而不是像这样设置它。首先,您将在.xaml.cs文件中实现INotifyPropertyChanged,如

public class YourClassName: Window, INotifyPropertyChanged
{
   public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

   //then create a string variable in your .xaml.cs file like   

    private string _logText;

    public string LogText
    {
         get{ return _logText;}
         set { _logText = value; OnPropertyChanged("LogText"); }
    }

    public YourClassName()
    {
         InitializeComponent();
         //setting data context of the window
         this.DataContext = this;
    }
}

在您的XAML中,使用:

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

现在,您只需更新类中的LogText变量,如

this.LogText = this.LogText + "Initialising installation..." + "\r\n";  //or better use StringBuilder and Append function