回调类更新wpf UI

时间:2014-05-26 18:21:21

标签: c# wpf wcf user-interface

我正在使用WCF-Duplex和WPF创建聊天应用。当从服务器调用回调方法(在UI之外的另一个类中)时,是否有任何方法可以调用UI函数?!

以下是我的课程示例:

服务:

[ServiceContract( 
    Name = "GPH_QuickMessageService",
    Namespace = "TextChat",
    SessionMode = SessionMode.Required,
    CallbackContract = typeof(IMessageServiceCallback))]

        public interface IMessageServiceInbound
        {
            [OperationContract]
            int JoinTheConversation(string userName);

        }   public interface IMessageServiceCallback
        {
            [OperationContract(IsOneWay = true)]
            void NotifyUserJoinedTheConversation(string userName);
        }

JoinTheConversation在客户端调用NotifyUserJoinedTheConversation方法

客户: 表格:

           public partial class Account : Window 
    {
      public Account()
        {
           InitializeComponent();

        }
      public void updateUsersInConversation(string username)
       {
             TreeViewItem item = new TreeViewItem();
             item.Header = username;
             contactsTree.Children.Add(item);
       }
   }

客户端的回调实现

[CallbackBehavior(UseSynchronizationContext = false)]
public class ChatCallBack : GPH_QuickMessageServiceCallback, IDisposable
{

    public ChatCallBack()
    {
        //UIContext = context;
    }

    public void NotifyUserJoinedTheConversation(string userName)
    {
        MessageBox.Show("IN CLIENT");
        //I want to call updateUsersInConversation in the UI class
    }
  }

我搜索了很多,发现了很多关于委托和SendOrPostCallBack的事情,但我无法将所有这些事情联系在一起。对于这篇长篇文章我很抱歉,希望有人能帮助我

2 个答案:

答案 0 :(得分:1)

您可以在帐户类中注册的事件处理程序中尝试使用Dispatcher.Invoke()

此处有更多信息:How to update UI from another thread running in another class

[编辑]一些代码示例:

class ChatCallBack 
{
    public event EventHandler<string> UserJoinedTheConversation;

    public void NotifyUserJoinedTheConversation(string username)
    {
        var evt = UserJoinedTheConversation;
        if (evt != null)
            evt(this, username);
    }

    //other code
}

在您的帐户类中:

private ChatCallBack chatCallBack;
public Account() //class constructor
{
    InitializeComponent();
    chatCallBack = new ChatCallBack();
    chatCallBack.UserJoinedTheConversation += (sender, username) =>
    {
        Dispatcher.Invoke(() => updateUsersInConversation(username));
    };
}

答案 1 :(得分:1)

你可以这样做,

创建一个返回MainWindow实例的方法

 public static Account _this;

        public Account()
        {
            InitializeComponent();
            _this = this;

        }
        public void updateUsersInConversation(string username)
        {
            TreeViewItem item = new TreeViewItem();
            item.Header = username;
            //contactsTree.Children.Add(item);
        }
        public  static Account GetInstance()
        {
            return _this;
        }

在您的客户端回调课程中,您可以调用此方法

        public void NotifyUserJoinedTheConversation(string userName)
        {
          Account temp = Account.GetInstance();
          temp.updateUsersInConversation("test");
        }