[英]EventArgs does not contain a constructor that takes 1 argument
I can't find a specific problem that matches mine, which I think has to do with the fact that I am creating a subclass of EventArgs with one argument, a string. 我找不到与我的匹配的特定问题,我认为这与我使用一个参数(字符串)创建EventArgs的子类这一事实有关。 When I try to compile, it seems to tell me that ScanInfoEventArgs doesn't have one constructor, when it clearly does (clearly to me at least).
当我尝试编译时,似乎告诉我ScanInfoEventArgs没有一个构造函数,但显然没有(至少在我看来)。
I've only included the bit of code that I think applies. 我只包含了一些我认为适用的代码。 It seems like such a simple thing that I am at a loss.
看起来如此简单,我很茫然。
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);
}
}
}
You need to make your constructor public
. 您需要将构造函数
public
。 All class members default to private
, which means the outside world can't get at them. 所有班级成员默认为
private
,这意味着外界无法与他们接触。
Since the compiler didn't see a matching public constructor (as in, one the code could actually invoke), it threw the error you saw. 由于编译器没有看到匹配的公共构造函数(例如,代码实际上可以调用的构造函数),因此抛出了您看到的错误。
Correct code: 正确的代码:
public ScanInfoEventArgs(string scanType)
{
this.scanType = scanType;
}
Note that internal
would work as well if all code resides in the same assembly. 请注意,如果所有代码都驻留在同一程序集中,则
internal
也会正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.