我找不到与我匹配的特定问题,我认为这与我使用一个参数,一个字符串创建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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
你需要建立你的构造函数public.所有班级成员都默认为private,这意味着外界无法得到他们.
由于编译器没有看到匹配的公共构造函数(例如,代码实际可以调用的一个),它抛出了您看到的错误.
正确的代码:
public ScanInfoEventArgs(string scanType)
{
this.scanType = scanType;
}
Run Code Online (Sandbox Code Playgroud)
请注意,internal如果所有代码都驻留在同一个程序集中,那么也可以.
| 归档时间: |
|
| 查看次数: |
953 次 |
| 最近记录: |