[英]WPF MVVM Console Output to a TextBox within a View
我正在編寫一個更新文件的應用程序,然后最終將更新的文件導入數據庫。 我想顯示有關正在更新哪個文件以及該過程何時完成的消息。 我希望消息來自控制台,因為最終我使用的導入程序庫會通過控制台顯示有用的消息,我也想顯示這些消息。 我之前在 WPF 應用程序中能夠做到這一點,但我的所有代碼都在視圖后面的代碼中,我想保留 MVVM 模式並將代碼分離到一個 ViewModel 中。 我的問題是我不知道如何獲得對我視圖中的 TextBox 的引用。 一旦我能夠在我的 ViewModel 中獲取文本框,我就可以將控制台寫入發送到文本框。
這是我的觀點
<Window x:Class="DICOM_Importer.Views.StudyImporterView"
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:DICOM_Importer.Views"
mc:Ignorable="d"
Background="Gold"
Title="Importer" Height="450" Width="800">
<Grid Style="{StaticResource gridBackground}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="125" />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="280" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Orientation="Horizontal">
<Label Style="{Binding Source={StaticResource studyTitle}}" Content="Study:" />
<Label Style="{Binding Source={StaticResource studyTitle}}" Name="StudyImportViewStudyText" Content="{Binding ImporterTitle}" />
</StackPanel>
<StackPanel Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" Orientation="Horizontal" >
<Label Style="{Binding Source={StaticResource studyTitle}}" Content="Import Directory" />
<Label Style="{Binding Source={StaticResource studyTitle}}" Content="{Binding ImporterPath}" />
</StackPanel>
<Button Grid.Column="2" Grid.Row="1" Command="{Binding ImportCommand}" Style="{Binding Source={StaticResource buttonStyleImport}}" Content="Submit" />
<TextBox Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="2" x:Name="ImportConsole" />
</Grid>
</Window>
這是視圖模型
using DICOM_Importer.Commands;
using DICOM_Importer.Views;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
namespace DICOM_Importer.ViewModels
{
public class StudyImporterViewModel : INotifyPropertyChanged, IDataErrorInfo
{
private string importerTitle;
private string importerPath;
public DirectoryInfo[] directories;
GetIndividualSubjectDirectories subjectDirectories = new GetIndividualSubjectDirectories();
ConsoleOutputStream outputter;
/// <summary>
/// Gets the study information from the HomeView
/// </summary>
public String ImporterTitle
{
get { return importerTitle; }
set
{
importerTitle = value;
OnPropertyChanged("ImporterTitle");
}
}
public String ImporterPath
{
get { return importerPath; }
set
{
importerPath = value;
OnPropertyChanged("ImporterPath");
}
}
public StudyImporterViewModel()
{
ImportCommand = new ActivateImport(this);
outputter = new ConsoleOutputStream(ImportConsole); //Here is where the error is
Console.SetOut(outputter);
}
public ICommand ImportCommand
{
get;
private set;
}
public void Import()
{
MessageBoxResult result = MessageBox.Show("This will import every series in the Import Directory. Are you sure you want to Import?", "Import Confirmation", MessageBoxButton.OKCancel);
switch (result)
{
case MessageBoxResult.OK:
if(importerTitle == "SPIROMICS2")
{
Console.WriteLine("Importing SPIROMICS2 Scans to Mifar");
directories = subjectDirectories.GetSubjectDirectories(importerPath);
subjectDirectories.GetSeriesDirectories(directories);
Console.WriteLine("Import Complete");
}
else if(importerTitle == "BLF")
{
Console.WriteLine("BLF");
}
else if(importerTitle == "PRECISE")
{
Console.WriteLine("PRECISE");
}
break;
case MessageBoxResult.Cancel:
MessageBox.Show("CANCEL", "Nope!");
break;
}
}
#region Error Model
public string Error
{
get;
set;
}
#endregion
#region Error Definition
public string this[string columnName]
{
get
{
if (columnName == "ImporterTitle")
{
if (String.IsNullOrWhiteSpace(ImporterPath))
{
Error = "There is no selected study to import";
}
else
{
Error = null;
}
}
return Error;
}
}
#endregion
#region PropertyChangedEventHandler
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
這是我的 ConsoleOutputStream 命令
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
namespace DICOM_Importer.Commands
{
class ConsoleOutputStream : TextWriter
{
TextBox textBox = null;
public ConsoleOutputStream(TextBox ouput)
{
textBox = ouput;
}
public override void Write(char value)
{
base.Write(value);
textBox.Dispatcher.BeginInvoke(new Action(() => {
textBox.AppendText(value.ToString());
}));
}
public override Encoding Encoding
{
get { return System.Text.Encoding.UTF8; }
}
}
}
這是按鈕的命令,它將啟動所有文件更改和導入
using DICOM_Importer.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace DICOM_Importer.Commands
{
/// <summary>
/// Starts the import processes on a button click if there is a study available
/// </summary>
class ActivateImport : ICommand
{
private StudyImporterViewModel _studyImporterViewModel;
public ActivateImport(StudyImporterViewModel viewModel)
{
_studyImporterViewModel = viewModel;
}
public event EventHandler CanExecuteChanged
{
//this is forcing the CommandManager to check the ICommand again. If we didn't have this then the buitton wouldl only be
//disabled if the window was loaded with a blank name, not if it was loaded with a name and then was deleted by the user
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public bool CanExecute(object parameter)
{
return String.IsNullOrWhiteSpace(_studyImporterViewModel.Error);
}
public void Execute(object parameter)
{
_studyImporterViewModel.Import();
}
}
}
任何幫助將非常感激!
您可以將控制台輸出重定向到StringWriter
,然后在每次生成新的控制台輸出時將其內容寫入綁定到文本框的字符串。
視圖模型
private StringWriter _sw;
public string ConsoleOut { /* getter and setter */ }
// inside constructor
_sw = new StringWriter();
Console.SetOut(sw);
Console.SetError(sw);
xml
<TextBlock Text="{Binding Path=ConsoleOut, Mode=OneWay}"/>
這里的問題是每次要在 TextBlock 中顯示控制台輸出時,都需要使用S = _sw.ToString();
更新 S 的值S = _sw.ToString();
解決方案
我通過增強型StringWriter
類的實現找到了這個答案,該類在每次寫入時都會觸發一個事件。 有了這個,你只需要做一個簡單的更新:
視圖模型
private StringWriterExt _sw;
public string ConsoleOut { /* getter and setter */ }
// inside constructor
_sw = new StringWriterExt(true);
Console.SetOut(sw);
Console.SetError(sw);
_sw.Flushed += (s, a) => ConsoleOut = _sw.ToString();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.