2017-01-28 3 views
0

私は窓は、私が使用して更新するテキストボックスを含むされ、私はインストールの進行状況を表示するには...更新のTextBoxコンテンツ

をインストールを実行していた中で、WPFのプロジェクトを抱えています例えば:これは一種の作品

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

...

問題は、インストールが完了したら、のTextBoxのコンテンツのみが表示なっていることです。

私は今のようないくつかのsoluionsを、試してみました

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

私に実用的な解決策を教えたり、別の質問に私を指摘したりすることができます。すでにこの質問について議論していますが、非常に感謝しています。

+0

おかげで... – XaverXor

+0

あなたはUIスレッドでのインストールを実行しているようですが、音やプロセスが終了すると、あなたのUIが最初に更新される理由です。バックグラウンドスレッドで実行するか、finish_start_button_Clickイベントハンドラでasync/await(/ dispatcher)-conceptを使用してみてください。 –

答えて

0

UIスレッドで長いタスクを使用する場合、UIスレッドは他のコンテンツコントロールを更新するためにアイドル状態ではありません。 他のスレッドを作成してタスクを処理してスレッドにし、UIスレッドを使用してUIコントロールを更新する必要があります。再び尋ねたときに、私は心の中でそれを維持します@rene

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

この

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

ようですが、代わりにこのようにそれを設定するバインディングを使用する方がよいでしょうTextBlockのではなく、テキストボックスを使用してみてください。今

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

は、あなただけの

this.LogText = this.LogText + "Initialising installation..." + "\r\n"; //or better use StringBuilder and Append function 
のようなあなたのクラス内LogText変数を更新することができます:まず、あなたの.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、使用中のファイルを実装します

+0

これまでのようなことは一度もありませんでしたので、コンストラクタの意味を説明できますか(TextBlockにテキストを追加するコードの一部ですか?)実際にLogOutputにテキストを追加しますか?... @Ali Baig – XaverXor

+0

私は上記の私の答えを更新し、コンストラクタコードを追加しました。 –

+1

@ XaverXorあなたが知っているように、クラスのコンストラクタは、クラスに基づいてオブジェクトをインスタンス化するときに実行されるものです。上記の例では、DataContextプロパティはインスタンス化時に設定されます。 –

関連する問題