【问题标题】:SignalR Client with Multiple Parameters具有多个参数的 SignalR 客户端
【发布时间】:2017-03-07 02:06:37
【问题描述】:

我是 stackoverflow 的新手,多年来一直是潜伏者,对我作为开发人员的帮助很大。非常感谢。

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

情景:

我正在使用 SignalR。

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

当我在 Web 客户端 (MVC) 中实现它时,它运行良好,我可以获得所有这 6 个参数。

我尝试在 Xamarin 中实现它。

这里是示例代理 sn-p:

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 个参数时我都会收到此错误?

我错过了什么吗?

谢谢!

【问题讨论】:

    标签: c# android asp.net-mvc xamarin signalr


    【解决方案1】:

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

    解决方案非常简单。创建一个包含尽可能多的属性的类,而不是使用参数。比如:

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

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

    【讨论】:

    • 我明白了。所以这是一个限制。这就说得通了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    • 1970-01-01
    • 2016-06-16
    • 2021-08-12
    • 2017-12-31
    • 1970-01-01
    相关资源
    最近更新 更多