繁体   English   中英

EventArgs不包含带有1个参数的构造函数

[英]EventArgs does not contain a constructor that takes 1 argument

我找不到与我的匹配的特定问题,我认为这与我使用一个参数(字符串)创建EventArgs的子类这一事实有关。 当我尝试编译时,似乎告诉我ScanInfoEventArgs没有一个构造函数,但显然没有(至少在我看来)。

我只包含了一些我认为适用的代码。 看起来如此简单,我很茫然。

 public partial class MainWindow : Window
{
    Coffee coffeeOnHand;
    SweetTea sweetTeaOnHand;
    BlueberryMuffin blueberryMuffinOnHand;

    public MainWindow()
    {
        InitializeComponent();

        //The following reads the inventory from file, and assigns each inventory item to the Coffee, SweatTea 
        //and BlueberryMuffin objects in memory.
        using (Stream input = File.OpenRead("inventory.dat"))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            coffeeOnHand = (Coffee)formatter.Deserialize(input);
            sweetTeaOnHand = (SweetTea)formatter.Deserialize(input);
            blueberryMuffinOnHand = (BlueberryMuffin)formatter.Deserialize(input);
        }

        //The following adds whatever information is loaded from the objects on file from above
        //into the dropdown box in the menu.
        SelectedItemDropdown.Items.Add(coffeeOnHand);
        SelectedItemDropdown.Items.Add(sweetTeaOnHand);
        SelectedItemDropdown.Items.Add(blueberryMuffinOnHand);
    }

    public class ScanInfoEventArgs : EventArgs
    {
        ScanInfoEventArgs(string scanType)
        {
            this.scanType = scanType;
        }
        public readonly string scanType;
    }

    public class Scan
    {
        //Delegate that subscribers must implement
        public delegate void ScanHandler (object scan, ScanInfoEventArgs scanInfo);

        //The event that will be published
        public event ScanHandler onScan;

        public void Run()
        {
            //The ScanInfoEventArgs object that will be passed to the subscriber.
            ScanInfoEventArgs scanInformation = new ScanInfoEventArgs("scanType");

            // Check to see if anyone is subscribed to this event.
            if (onScan != null)
            {
                onScan(this, scanInformation);
            }
        }
    }

您需要将构造函数public 所有班级成员默认为private ,这意味着外界无法与他们接触。

由于编译器没有看到匹配的公共构造函数(例如,代码实际上可以调用的构造函数),因此抛出了您看到的错误。

正确的代码:

    public ScanInfoEventArgs(string scanType)
    {
        this.scanType = scanType;
    }

请注意,如果所有代码都驻留在同一程序集中,则internal也会正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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