繁体   English   中英

从 SignalR 客户端打开 WinForm

[英]Open WinForm from SignalR Client

我使用了这个示例 [GitHub SignalR 示例] https://github.com/nthdeveloper/SignalRSamples
它使用 winforms 作为 signalR 服务器
它使用两个客户端 winforms 和一个 javascript
它 append 文本框中的客户端消息

   private void SimpleHub_MessageReceived(string senderClientId, string message)
    {
        //One of the clients sent a message, log it
        this.BeginInvoke(new Action(() =>
        {
            string clientName = _clients.FirstOrDefault(x => x.Id == senderClientId)?.Name;

            writeToLog($"{clientName}:{message}");
        }));
    }

我需要根据消息打开一个表单

  private void SimpleHub_MessageReceived(string senderClientId, string message)
    {
        //One of the clients sent a message, log it
        this.BeginInvoke(new Action(() =>
        {
            string clientName = _clients.FirstOrDefault(x => x.Id == senderClientId)?.Name;
            switch (message)
            {
                case "form1":
                    Form1 frm = new Form1();
                    frm.Show();
                    break;
                case "form2":
                   Form2 frm = new Form();
                    frm.Show();
                    break;

                default:
                    // code block
                    break;
            }
          
          
        }));
    }

我已经尝试了表单打开并保持加载的代码我无法与之交互缺少什么

因为Form.Show不会立即阻止导致 go 从 scope 出来。

您需要使用Form.ShowDialog 这将阻止允许表单的生命周期在离开 scope 之前完成。

话虽如此,试试这个:

switch (message)
{
    case "form1":
        using(var frm = new Form1())
        {
            frm.ShowDialog(this);
        }
        break;
    case "form2":
        using(var frm = new Form2())
        {
            frm.ShowDialog(this);
        }
        break;
    default:
        // code block
        break;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM