我是一名 C# 程序員,并制作了一個 C# 庫來處理 tcp/ip 聊天?,F在我有必要從 Server VB Winform 程序中使用它。我很確定這是一個簡單的解決方案問題,但我已經掙扎了好幾天。所以在 C# 我有那個類:public class AsynchronousServer{ public AsynchronousServer() { } public delegate void ChangedEventHandler(string strMessage); public static event ChangedEventHandler OnNotification; public static event ChangedEventHandler OnAnswerReceived; ...}現在我必須專注于服務器程序:如果那個 VB 程序是在 C# 中,我會編寫下面的代碼,用于通過單擊按鈕連接服務器public partial class MainWindow : Window{ public MainWindow() { InitializeComponent(); } private void btnStart_Click(object sender, RoutedEventArgs e) { AsynchronousServer.OnNotification += AsynchronousServer_OnNotification; AsynchronousServer.OnAnswerReceived += AsynchronousServer_OnAnswerReceived; AsynchronousServer.StartServer(tbxIpAddress.Text,tbxPort.Text); }VB中的相同程序:Imports SocketLibrary Public Class Form1 Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click AsynchronousServer.OnNotification <--- member not present AsynchronousServer.OnAnswerReceived <--- member not present AsynchronousServer.StartServer(tbxIpAddress.Text, tbxPort.Text);<-----OK End SubEnd Class問題是成員 AsynchronousServer.OnNotification 根本不存在,所以我無法添加事件。我知道我可能需要添加 WithEvents 關鍵字,但我可能無法成功。簡而言之,我必須將該 VB winform 程序連接到我無法從 VB 類中看到的 C# 庫事件。
2 回答

搖曳的薔薇
TA貢獻1793條經驗 獲得超6個贊
我假設您從未使用過 VB,因此會回答您:
您需要使用AddHandler
:
AddHandler AsynchronousServer.OnNotification, AddressOf YourMethod
順便說一句,恕我直言,靜態事件非常糟糕。
- 2 回答
- 0 關注
- 254 瀏覽
添加回答
舉報
0/150
提交
取消