[英]How to pass value/parameter into a “public delegate void”?
請幫我解決一下這個。 假設我在這里有這個代碼......
void IScanSuccessCallback.barcodeDetected(MWResult result)
{
if (result != null)
{
try
{
var scan = Element as BarcodeScannerModal;
if (scan == null)
return;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
}
......我希望將MWResult結果的值傳遞給這個......
[System.Runtime.InteropServices.ComVisible(true)]
public delegate void EventScanHandler(MWResult result);
我真的遇到了這個問題。
如何使用基本委托void。 委托void是引用對象或void的方法。 它幾乎就像一個占位符,如果你有兩個相同的功能,你需要切換功能,而不是數字(將5 + 5中的+轉換為*,如5 * 5 ,下面的示例顯示如何這樣做。 它也必須是一個int作為一個返回!你也可以將它轉換為空白以便執行功能 )。 在你的情況下,你想要掃描條形碼,然后檢查它是否為null
所以我很困惑你為什么要使用委托空。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
//delegates are mostly defined outside of the class
public delegate int addn(int num1, int num2);
//the main class
class Program
{
//delegate functions
public static int add1(int num1, int num2)
{
return num1 + num2;
}
public static int add2(int num1, int num2)
{
return num1 * num2;
}
//entry
public static void Main()
{
//here we can reference delegates as a class. its
//called addf in this case.
//first we init add1
addn addf = new addn(add1);
Console.WriteLine(addf(5, 5));
//then we innit add2. note we dont add /*addn*/ in the beginning because
//its already defined
addf = new addn(add2);
Console.WriteLine(addf(5, 5));
//loop
while (true) ;
}
}
}
除非你使用的是不同版本的IScanSuccessCallback.barcodeDetected,我只是直接調用它,將值存儲在一個類中,然后使用[System.Runtime.InteropServices.ComVisible(true)] public delegate void EventScanHandler(
Class name result);
委托是表示方法引用的類型。
ScanSuccessCallbackImpl t = new ScanSuccessCallbackImpl();
EventScanHandler method = t.barcodeDetected;
method(new MWResult());
這是否回答了你的問題?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.