gpt4 book ai didi

c# - 如何将值/参数传递到 "public delegate void"中?

转载 作者:行者123 更新时间:2023-11-30 16:42:32 26 4
gpt4 key购买 nike

请帮我解决这个问题。假设我这里有这段代码......

    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);

我真的遇到了这个问题。

最佳答案

如何使用基本委托(delegate) void。委托(delegate) void 是引用对象或 void 的位置的方法。它几乎就像一个占位符,如果你有两个相同的功能并且你需要切换功能,而不是数字(将 5+5 中的 + 切换为 *,如 5*5,下面的例子展示了如何做到这一点。它也必须是一个 int 作为返回值!你也可以将它换成 void 以执行函数 ).在你的情况下,你想扫描条形码然后检查它是否为 null 所以我很困惑你为什么要使用委托(delegate) void。

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(
类名result);

关于c# - 如何将值/参数传递到 "public delegate void"中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46578838/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com