【发布时间】:2018-02-16 11:56:51
【问题描述】:
好的,所以我目前正在尝试将一些数据绑定到我的应用程序中的文本框,这就是它的样子。 我有一个 Page.xaml,上面有一个文本框(还有其他一些东西,但我们不关心这些,因为我们只想将数据绑定到文本框而不是其他东西)
我有一个名为 Server 的类,该类负责启动 .jar 文件和其他一些小事。 当它启动进程时,它会启动一个 BAT 文件,你知道,一个 cmd 窗口。 然后我已经将它重定向到输出重定向的位置,因此无论打印到我们想要捕获的 cmd 窗口并附加到 PlayPage.xaml 的文本框中,这是我之前提到的页面。
我遇到的问题是我想将来自 cmd 窗口的任何内容数据绑定到我的文本框,这是可能的,因为我以前做过,但不是通过使用数据绑定。
换句话说。发送到 cmd 窗口的文本我希望将其重定向到我的文本框。
这是一张显示打印到 cmd 窗口的文本的图片(它会随着时间的推移不断打印更多内容)
这是项目的结构
这里是 XAML
<TextBox x:FieldModifier="public" Name="TbConsoleOutput" Background="#262626" Foreground="GreenYellow" HorizontalAlignment="Left" Height="141" Margin="10,33,0,0" TextWrapping="NoWrap" Text="{Binding Output}" VerticalAlignment="Top" Width="660"/>
这是课程
using System.Diagnostics;
using System.IO;
using System.Windows;
using CraftaServ.Pages;
using Microsoft.Win32;
namespace CraftaServ.Classes
{
public class Server
{
private readonly PlayPage PlayPage;
public int ProcessID;
public Process ServerProcess = new Process();
public string Output { get; set; }
public void StartServer()
{
var ofd = new OpenFileDialog();
ofd.Filter = "Server File | *.jar";
if (ofd.ShowDialog() == true)
{
var IsStarted = false;
var FilePath = Path.GetFileName(ofd.FileName);
var StartInfo = new ProcessStartInfo("java", $"-Xmx1024M -jar craftbukkit-1.12.2.jar -o true");
StartInfo.UseShellExecute = false;
StartInfo.RedirectStandardError = true;
StartInfo.RedirectStandardInput = true;
StartInfo.RedirectStandardOutput = true;
StartInfo.CreateNoWindow = false;
ServerProcess.StartInfo = StartInfo;
ServerProcess.OutputDataReceived += ServerProcess_OutputDataReceived;
ServerProcess.ErrorDataReceived += ServerProcess_ErrorDataReceived;
ServerProcess.Start();
ServerProcess.BeginOutputReadLine();
ServerProcess.BeginErrorReadLine();
ProcessID = ServerProcess.Id;
IsStarted = true;
while (IsStarted)
if (ServerProcess.HasExited)
{
IsStarted = false;
MessageBox.Show("Process has exited! Process ID: " + ProcessID);
}
}
}
private void ServerProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Application.Current.Dispatcher.Invoke(() => { Output = e.Data; });
//I want to append e.Data to the textbox in on the PlayPage.xaml which is where the Textbox is located
}
private void ServerProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Application.Current.Dispatcher.Invoke(() => { Output = e.Data; });
//I want to append e.Data to the textbox in on the PlayPage.xaml which is where the Textbox is located
}
}
}
【问题讨论】:
标签: c# .net wpf xaml data-binding