shouxinger 2022-02-10 22:34 采纳率: 0%
浏览 51

C#关于委托的一行代码 需要转换为VB.NET代码

我对委托不熟,希望各位帮忙一下

问题遇到的现象和发生背景

西门子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)

img

我想要达到的结果

我需要把这段C#转换为VB.NET,就差这两句委托。

  • 写回答

2条回答 默认 最新

  • 丨Haruna 2022-02-11 10:35
    关注
    AddHandler myClientHelperAPI.KeepAliveNotification, AddressOf Notification_KeepAlive
    
    评论 编辑记录

报告相同问题?

问题事件

  • 修改了问题 2月10日
  • 创建了问题 2月10日

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来