您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
在WinForms应用程序中,窗体间通信可以通过多种方式实现。以下是一些常见的方法:
你可以在一个窗体中定义一个事件,并在另一个窗体中订阅这个事件。
// Form1.cs
public partial class Form1 : Form
{
public event EventHandler<string> MessageSent;
public Form1()
{
InitializeComponent();
}
private void buttonSend_Click(object sender, EventArgs e)
{
MessageSent?.Invoke(this, "Hello from Form1");
}
}
// Form2.cs
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public Form2(Form1 form1)
{
InitializeComponent();
form1.MessageSent += Form1_MessageSent;
}
private void Form1_MessageSent(object sender, string message)
{
MessageBox.Show(message);
}
}
你可以在一个窗体中定义一个共享变量,并在另一个窗体中访问这个变量。
// Form1.cs
public partial class Form1 : Form
{
private string sharedMessage = "";
public Form1()
{
InitializeComponent();
}
private void buttonSend_Click(object sender, EventArgs e)
{
sharedMessage = "Hello from Form1";
MessageBox.Show("Message sent to Form2");
}
}
// Form2.cs
public partial class Form2 : Form
{
public Form2(Form1 form1)
{
InitializeComponent();
form1.sharedMessageChanged += Form1_SharedMessageChanged;
}
private void Form1_SharedMessageChanged(object sender, string message)
{
MessageBox.Show(message);
}
}
你可以定义一个接口,并在窗体中实现这个接口。
// IMessageListener.cs
public interface IMessageListener
{
void OnMessageReceived(string message);
}
// Form1.cs
public partial class Form1 : Form, IMessageListener
{
public Form1()
{
InitializeComponent();
}
public void OnMessageReceived(string message)
{
MessageBox.Show(message);
}
private void buttonSend_Click(object sender, EventArgs e)
{
MessageBox.Show("Message sent to Form2");
}
}
// Form2.cs
public partial class Form2 : Form
{
private IMessageListener messageListener;
public Form2(IMessageListener listener)
{
InitializeComponent();
messageListener = listener;
}
private void buttonSend_Click(object sender, EventArgs e)
{
messageListener.OnMessageReceived("Hello from Form2");
}
}
你可以使用单例模式来共享数据或逻辑。
// MessageManager.cs
public class MessageManager : Singleton<MessageManager>
{
private string sharedMessage = "";
protected MessageManager() { }
public static MessageManager Instance => GetInstance();
public void SetMessage(string message)
{
sharedMessage = message;
}
public string GetMessage()
{
return sharedMessage;
}
}
// Form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonSend_Click(object sender, EventArgs e)
{
MessageManager.Instance.SetMessage("Hello from Form1");
MessageBox.Show("Message sent to Form2");
}
}
// Form2.cs
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void buttonSend_Click(object sender, EventArgs e)
{
MessageBox.Show(MessageManager.Instance.GetMessage());
}
}
你可以使用消息传递机制,如WPF中的Messenger
类。
// Messenger.cs
public static class Messenger
{
private static readonly ConcurrentDictionary<Type, Action<object>> _handlers = new ConcurrentDictionary<Type, Action<object>>();
public static void Register<TMessage>(Action<TMessage> handler) where TMessage : class
{
_handlers.AddOrUpdate(typeof(TMessage), handler, (t, h) => handler);
}
public static void Unregister<TMessage>() where TMessage : class
{
_handlers.TryRemove(typeof(TMessage), out _);
}
public static void Send<TMessage>(TMessage message) where TMessage : class
{
_handlers[typeof(TMessage)]?.Invoke(message);
}
}
// Form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Messenger.Register<string>(message => MessageBox.Show(message));
}
private void buttonSend_Click(object sender, EventArgs e)
{
Messenger.Send("Hello from Form1");
}
}
// Form2.cs
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
}
选择哪种方法取决于你的具体需求和应用程序的复杂性。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。