在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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。