簡體   English   中英

回調類以更新wpf UI

[英]callback class to update wpf UI

我正在使用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的很多內容,但是我無法將所有這些內容鏈接在一起。 很長的帖子,我很抱歉,希望任何人都可以幫助我

您可以在Account類中注冊的事件處理程序中嘗試使用Dispatcher.Invoke()

這里的更多信息: 如何從另一個類中運行的另一個線程更新UI

[編輯]一些代碼示例:

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));
    };
}

你可以這樣做,

創建一個方法以返回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;
        }

在您的Client回調類中,您可以調用如下方法

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM