具有多个参数的 SignalR 客户端

SignalR Client with Multiple Parameters

我是Whosebug的新手,潜伏多年,对我这个开发者帮助很大。非常感谢。

以第一个 post 和问题结束我的介绍:

场景:

我正在使用 SignalR。

我有一个 SignalR 服务器,它使用 6 个参数向所有客户端广播一条消息。

当我在 Web 客户端 (MVC) 中实现它时,它工作正常并且我可以获得所有这 6 个参数。

我尝试在 Xamarin 中实现它。

这是示例代理片段:

proxy.On<string, string, string , string, string, string>("test", (test1, test2, test3, test4, test5, test6) =>
            {
                MyActivity.RunOnUiThread(() =>
                {
                    //my method here
                });
            });

当我有 6 个参数时,我会得到这个错误:

'IHubProxy' 不包含 'On' 的定义,并且找不到接受 'IHubProxy' 类型的第一个参数的扩展方法 'On' (您是否缺少 using 指令或程序集引用?)

但是当我将参数更改为 4

proxy.On<string, string, string , string>("test", (test1, test2, test3, test4) =>
        {
            MyActivity.RunOnUiThread(() =>
            {
                //my method here
            });
        });

我不会得到错误,我将能够得到这 4 个参数。但是在我的应用程序中,我需要获取所有这 6 个参数。

为什么每当我有超过 4 个参数时我都会收到此错误?

我是不是漏掉了什么?

谢谢!

这只是对 SignalR .NET 客户端代理的限制。开发人员似乎有点懒于覆盖 On 方法以支持更多类型参数,或者他们只是认为如果你有更多参数,你应该将它们分组在 class.

解决方法非常简单。创建一个包含所需属性的 class,而不是使用参数。类似于:

public class AllParams
{
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
    public string Prop3 { get; set; }
    public string Prop4 { get; set; }
    public string Prop5 { get; set; }
    public string PropN { get; set; }
}

proxy.On<AllParams>("test", all =>
{
    MyActivity.RunOnUiThread(() =>
    {
        // all.Prop1, all.Prop2, etc...
    });
});

这甚至可以提高代码的可读性。