![](/img/trans.png)
[英]Does not contain a constructor that takes 1 argument even though there is a constructor that takes 1 argument
[英]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.