SignalR 发送对象 - 新手

SignalR send object - newbie

使用 Signal R,如果尝试发送对象,传递模型的语法是什么?

private async void FormLoaded(object sender, RoutedEventArgs e)
{
    //Connection stuff...
    Proxy.On("sendHello", () => OnSendDataConnection(ModelBuilder()));
}

private void OnSendDataConnection(ConnectionModel model)
{
    Dispatcher.Invoke(DispatcherPriority.Normal,model?????)
    this.Dispatcher.Invoke((Action)(LoadW));
}

查看问题(文本,而不是代码)我了解到您正在尝试将 Complex 对象发送到 JS 端并在那里使用它?对吗?

在那种情况下,解决方案应该很简单:

来自ASP.NET.SignalR site

You can specify a return type and parameters, including complex types and arrays, as you would in any C# method. Any data that you receive in parameters or return to the caller is communicated between the client and the server by using JSON, and SignalR handles the binding of complex objects and arrays of objects automatically.

示例 C#:

    public void SendMessage(string name, string message)
    {
        Clients.All.addContosoChatMessageToPage(new ContosoChatMessage() { UserName = name, Message = message });
    }

JS:

    var contosoChatHubProxy = $.connection.contosoChatHub;
    contosoChatHubProxy.client.addMessageToPage = function (message) {
        console.log(message.UserName + ' ' + message.Message);
    });

ContosoChatMessage 所在位置:

   public class ContosoChatMessage
   {
       public string UserName { get; set; }
       public string Message { get; set; }
   }

(继续阅读示例...)

所以基本上,在 JS 中一旦 'model' 收到,你应该可以使用 'model.XY',其中 XY 是模型复杂对象的成员。

希望对您有所帮助。

就我而言,我需要将我的字段转换为属性,否则它无法正确发送信息。 来自

public string myField;

public string myField {get; set;}