繁体   English   中英

如何将值/参数传递给“public delegate void”?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM