我对委托不熟,希望各位帮忙一下
问题遇到的现象和发生背景
西门子OPC UA中的一段代码,我想转换为VB.NET,就这句。我该怎么转?
myClientHelperAPI.KeepAliveNotification += new KeepAliveEventHandler(Notification_KeepAlive);
问题相关代码,请勿粘贴截图
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Opc.Ua;
using Opc.Ua.Client;
using Siemens.UAClientHelper;
using System.Security.Cryptography.X509Certificates;
namespace BasicOPC_C
{
public partial class Form1 : Form
{
private Session mySession;
private Subscription mySubscription;
private UAClientHelperAPI myClientHelperAPI;
private EndpointDescription mySelectedEndpoint;
private MonitoredItem myMonitoredItem;
private List<String> myRegisteredNodeIdStrings;
private ReferenceDescriptionCollection myReferenceDescriptionCollection;
private List<string[]> myStructList;
private UAClientCertForm myCertForm;
private Int16 itemCount;
public Form1()
{
InitializeComponent();
myClientHelperAPI = new UAClientHelperAPI();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//Check if sessions exists; If yes > delete subscriptions and disconnect
if (mySession != null && !mySession.Disposed)
{
try
{
mySubscription.Delete(true);
}
catch
{
;
}
myClientHelperAPI.Disconnect();
mySession = myClientHelperAPI.Session;
}
else
{
try
{
//Register mandatory events (cert and keep alive)
myClientHelperAPI.KeepAliveNotification += new KeepAliveEventHandler(Notification_KeepAlive);
myClientHelperAPI.CertificateValidationNotification += new CertificateValidationEventHandler(Notification_ServerCertificate);
//Check for a selected endpoint
//if (mySelectedEndpoint != null)
//{
//Call connect
//myClientHelperAPI.Connect(mySelectedEndpoint, userPwButton.Checked, userTextBox.Text, pwTextBox.Text).Wait();
//myClientHelperAPI.Connect(mySelectedEndpoint, false, "User name", "Password").Wait();
//"opc.tcp://192.168.15.81:4840/"
myClientHelperAPI.Connect("opc.tcp://192.168.15.81:4840", "#None", MessageSecurityMode.None, false, "User name", "Password").Wait();
//Extract the session object for further direct session interactions
mySession = myClientHelperAPI.Session;
//UI settings
myCertForm = null;
//}
//else
//{
// MessageBox.Show("Please select an endpoint before connecting", "Error");
// return;
//}
}
catch (Exception ex)
{
myCertForm = null;
MessageBox.Show(ex.InnerException.Message, "Error");
}
}
}
/// <summary>
/// Global OPC UA event handlers
/// </summary>
#region OpcEventHandlers
private void Notification_ServerCertificate(CertificateValidator cert, CertificateValidationEventArgs e)
{
//Handle certificate here
//To accept a certificate manually move it to the root folder (Start > mmc.exe > add snap-in > certificates)
//Or handle via UAClientCertForm
if (this.InvokeRequired)
{
this.BeginInvoke(new CertificateValidationEventHandler(Notification_ServerCertificate), cert, e);
return;
}
try
{
//Search for the server's certificate in store; if found -> accept
X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509CertificateCollection certCol = store.Certificates.Find(X509FindType.FindByThumbprint, e.Certificate.Thumbprint, true);
store.Close();
if (certCol.Capacity > 0)
{
e.Accept = true;
}
//Show cert dialog if cert hasn't been accepted yet
else
{
if (!e.Accept & myCertForm == null)
{
myCertForm = new UAClientCertForm(e);
myCertForm.ShowDialog();
}
}
}
catch
{
;
}
}
private void Notification_MonitoredItem(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new MonitoredItemNotificationEventHandler(Notification_MonitoredItem), monitoredItem, e);
return;
}
MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
if (notification == null)
{
return;
}
//subscriptionTextBox.Text = "Item name: " + monitoredItem.DisplayName;
//subscriptionTextBox.Text += System.Environment.NewLine + "Value: " + Utils.Format("{0}", notification.Value.WrappedValue.ToString());
//subscriptionTextBox.Text += System.Environment.NewLine + "Source timestamp: " + notification.Value.SourceTimestamp.ToString();
//subscriptionTextBox.Text += System.Environment.NewLine + "Server timestamp: " + notification.Value.ServerTimestamp.ToString();
}
private void Notification_KeepAlive(Session sender, KeepAliveEventArgs e)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new KeepAliveEventHandler(Notification_KeepAlive), sender, e);
return;
}
try
{
// check for events from discarded sessions.
if (!Object.ReferenceEquals(sender, mySession))
{
return;
}
// check for disconnected session.
if (!ServiceResult.IsGood(e.Status))
{
// try reconnecting using the existing session state
mySession.Reconnect();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
//ResetUI();
}
}
#endregion
private void readValButton_Click(object sender, EventArgs e)
{
List<String> nodeIdStrings = new List<String>();
List<String> values = new List<String>();
nodeIdStrings.Add(readIdTextBox.Text);
//写多个变量测试读一次所需要要的时间,100个变量
try
{
values = myClientHelperAPI.ReadValues(nodeIdStrings);
readTextBox.Text = values.ElementAt<String>(0);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
}
}
}
运行结果及报错内容
我的解答思路和尝试过的方法
我转换成这个后,说有问题。
myClientHelperAPI.KeepAliveNotification += New KeepAliveEventHandler(Notification_KeepAlive)
我想要达到的结果
我需要把这段C#转换为VB.NET,就差这两句委托。