[英]how to handle shared event in VB.net class in C# form
我在VB.net中編碼了一個引發事件的WCF。 這些事件是共享的。 看起來像這樣:
Shared Event onMissingSnapshots()
這是電話:
Public Shared Sub FireMissingSnapshots()
RaiseEvent onMissingSnapshots()
End Sub
從WCF回調中調用實施:
Private Class StatusCallback
Implements ServiceReference.TravelSequenceCallback
Public Sub onMissingSnapshots() Implements ServiceReference.TravelSequenceCallback.onMissingSnapshots
cWCF_Moteur.FireMissingSnapshots()
End Sub
End Class
現在,我需要將此類集成到C#WinForm項目中。 Intellisense將看不到Shared事件,因此當我嘗試添加委托處理程序時,它將給出錯誤消息。 如何在我的C#項目中實現這些事件?
非常感謝您的時間和幫助
您的活動執行不佳,盡管我不是100%確信這是造成您問題的原因。 我只是嘗試了以下方法,它為我工作。 我在VB中創建了一個DLL項目,並添加了以下代碼:
Public Class Class1
Public Shared Event MissingSnapshots As EventHandler
Protected Shared Sub OnMissingSnapshots(e As EventArgs)
RaiseEvent MissingSnapshots(Nothing, e)
End Sub
End Class
這是用於聲明和引發事件的標准模式,除了通常使用Me
而不是Nothing
作為實例事件的發送者之外。 此外,由於您無法覆蓋Shared
方法,因此沒有必要聲明OnMissingSnapshots
方法為Overridable
,也沒有必要聲明其為Protected
。 這可能是要走的路:
Public Shared Event MissingSnapshots As EventHandler
Private Shared Sub OnMissingSnapshots(e As EventArgs)
RaiseEvent MissingSnapshots(Nothing, e)
End Sub
Public Shared Sub RaiseMissingSnapshots()
OnMissingSnapshots(EventArgs.Empty)
End Su
然后,我將C#WinForms應用程序項目添加到相同的解決方案中,並引用了VB項目。 我能夠毫無問題地添加以下代碼,包括Intellisense:
using System;
using System.Windows.Forms;
using ClassLibrary1;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Class1.MissingSnapshots += Class1_MissingSnapshots;
}
private void Class1_MissingSnapshots(object sender, EventArgs e)
{
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.