[英]Using my c# event in VB.NET
I have written a DLL in C#, and I would like to get callback from an event. 我已经用C#编写了一个DLL,我想从一个事件中获取回调。
In C# I dit it the following way: 在C#中,我通过以下方式对其进行区分:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using SevenZipControl;
namespace Utlility
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Zipper.ProgressEvent += ProgressChanged;
}
delegate void ProgressChangedCallback(int value);
private void ProgressChanged(object sender, ProgressChangedEventArgs e)
{
SetProgressBarValue(e.ProgressPercentage);
}
private void SetProgressBarValue(int progress)
{
if (this.progressBar1.InvokeRequired)
{
ProgressChangedCallback d = new ProgressChangedCallback(SetProgressBarValue);
this.Invoke(d, new object[] { progress });
return;
}
progressBar1.Value = progress;
}
I tried the same in VB.NET, but the IDE already complains about my AddHandler approach: 我在VB.NET中尝试了同样的方法,但是IDE已经抱怨我的AddHandler方法:
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.IO
Imports SevenZipControl
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler Zipper.ProgressEvent, AddressOf ProgressChanged(Zipper, ProgressChangedEventArgs)
End Sub
Private Sub ProgressChanged(sender As Object, e As ProgressChangedEventArgs)
SetProgressBarValue(e.ProgressPercentage)
End Sub
Private Sub SetProgressBarValue(progress As Integer)
ProgressBar1.Value = progress
End Sub
End Class
The C# implementation works fine, but in VB.NET, the IDE tells me that "Zipper" and "ProgressChangedEventArgs" are a type and cannot be used as an expression. C#实现工作正常,但是在VB.NET中,IDE告诉我“ Zipper”和“ ProgressChangedEventArgs”是一种类型,不能用作表达式。
Can somebody tell me what I am doing wrong here? 有人可以告诉我我在做什么错吗? Also, it tells me that
而且,它告诉我
You have too much information in the AddHandler
line. 您在
AddHandler
行中的信息太多。 Just do: 做就是了:
AddHandler Zipper.ProgressEvent, AddressOf ProgressChanged
The solution was much more difficult. 解决方案要困难得多。 I had to create an impromptou function:
我必须创建一个即兴函数:
Zipper.Compress(uPathIn, uPathOut, Function(l1, l2)
SetProgressBarValue(0)
End Function)
I am not sure if that is the cleanest solution, and the compiler warns me that the function Function (l1, l2) may not have a valid return value, but it does everything I wanted. 我不确定这是否是最干净的解决方案,编译器警告我函数Function(l1,l2)可能没有有效的返回值,但它可以执行我想要的一切。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.