[英]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.