简体   繁体   English

在内部类中设置WPF文本框值

[英]Set WPF Textbox Value in innerclass

It may be a bit of a noob question but I'm not an experienced programmer. 这可能是一个菜鸟问题,但我不是一个经验丰富的程序员。

I am using WCF in combination with WPF to create a chatroom with a GUI. 我将WCF与WPF结合使用,以使用GUI创建聊天室。 My problem is that I would like to use the callbackhandler to set the value of a textbox with incoming messages. 我的问题是我想使用callbackhandler设置传入消息的文本框的值。 Because this is an innerclass I cannot however call the textbox. 因为这是一个内部类,但是我不能调用文本框。 Does anybody know a solution to this? 有人知道解决方案吗?

namespace WPFClient

public partial class MainWindow : Window
{
    Service1Client s;
    public MainWindow()
    {
        InitializeComponent();
        InstanceContext site = new InstanceContext(new CallbackHandler());
        s = new Service1Client(site);
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Message m = new Message();
        m.Content = txtMessage.Text;
        m.User = txtName.Text;
        s.SendMessage(m);
    }

    public class CallbackHandler : IService1Callback
    {
        public void SendMessageToClients(Message m)
        {

            //I would like to call an alrdy generated textbox here to set its value, like txtMessageAll.Text("Setting text");
        }
    }
}

} }

Thanks! 谢谢!

As CallbackHandler is a custom class, you can pass the TextBox from MainWindow to this class through parameterised constructor, while creating the object. 由于CallbackHandler是一个自定义类,因此可以在创建对象时通过参数化构造函数将MainBox的TextBox传递给此类。 As the reference is being passed you can change the Text of that TextBox through your Callback Handler class also. 在传递引用时,您还可以通过Callback Handler类更改该TextBox的Text。

 public class CallbackHandler 
    {
        public TextBox textValue { get; set; }

        CallbackHandler(TextBox tb) {

            this.textValue = tb;

        }
        public void SendMessageToClients(Message m)
        {
            this.textValue.Text="some_message";
            //I would like to call an alrdy generated textbox here to set its value, like txtMessageAll.Text("Setting text");
        }
    }

And from your MainWindow class 并从您的MainWindow类

InstanceContext site = new InstanceContext(new CallbackHandler(txtboxMessageAll));

Where "txtboxMessageAll" is the TextBox which is already present in Xaml page. 其中“ txtboxMessageAll”是Xaml页面中已经存在的TextBox。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM