xSockets和Windows Phone 8 App Invoke方法

时间:2015-04-13 10:23:41

标签: c# windows-phone-8.1 xsockets.net

我正在使用xSockets在Windows Phone 8上创建一个简单的聊天应用程序,但是我遇到了以下问题,在运行它说"不包含调用方法的定义",我在某处读到我必须使用Dispatch,但也没有运气。

我的代码:

private async void Connect()
    {
        Connection = new XSocketClient(ServerURI, Origin, "chat");

        chatController = Connection.Controller("chat");

        //Handle incoming event from server: use Invoke to write to console from XSocket's thread
        chatController.On<string>("addMessage", message => this.Invoke((Action)(() =>
                       RichTextBoxConsole.Text =(String.Format("{0}" + Environment.NewLine, message))
                   )));

        try
        {
            Connection.Open();
            comboBoxLocation.SelectedIndex = 0;

            await chatController.SetProperty("username", UserName);
        }
        catch
        {
            StatusText.Text = "Unable to connect to server: Start server before connecting clients.";
            No connection: Don't enable Send button or show chat UI
            return;
        }
    }

1 个答案:

答案 0 :(得分:0)

看起来您刚刚将XVA-07-03的WinForms示例复制到WindowsPhone 8.1项目中。

我添加了一个WP 8.1项目进行测试,你需要的代码就是这个而不是你的&#34; Invoke&#34;东西

chatController.On<string>("addMessage", message => this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
    this.Messages.Text += String.Format("{0}" + Environment.NewLine, message);
}));