【问题标题】:How do I databind text to my textbox from a seperate class如何从单独的类将文本数据绑定到我的文本框
【发布时间】: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


    【解决方案1】:

    在你的 Server 类中实现接口 INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    

    然后在您的“输出”设置器中调用该方法 NotifyPropertyChanged();

    根据您的其余代码,您可能需要将视图的数据上下文直接设置为服务器类或您的 xaml.cs 类本身,然后将您的文本数据绑定更改为属性 Server.Output

    【讨论】:

    • 我不太明白,介意再解释一下吗?
    • 要让您的数据绑定正常工作,您需要几件事:一个有效的数据上下文(在哪里可以找到您在 xaml 文件中编写的绑定)和数据上下文中的数据,这些数据会在更新时通知用户界面(通过实现 InotifyPropertyChanged 接口并在设置要反映在用户界面中的属性值时显式调用此方法)
    • 我没有在您的 Server 类顶部看到私有只读 PlayPage PlayPage 属性。链接也必须以其他方式完成:您的 PlayPage 必须在某处引用服务器类以访问“输出”属性。
    猜你喜欢
    • 1970-01-01
    • 2020-01-03
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-12
    • 2022-08-02
    • 2019-11-26
    相关资源
    最近更新 更多