gpt4 book ai didi

c# - EventArgs 不包含采用 1 个参数的构造函数

转载 作者:太空宇宙 更新时间:2023-11-03 19:05:19 25 4
gpt4 key购买 nike

我找不到与我的匹配的特定问题,我认为这与我正在创建 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 构造函数(代码实际上可以调用的构造函数),它抛出了您看到的错误。

正确的代码:

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

请注意,如果所有代码都位于同一个程序集中,internal 也可以正常工作。

关于c# - EventArgs 不包含采用 1 个参数的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28844577/

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