一直试图把我的头绕在代表身上,但当它陷入困境时似乎总是撞到一堵砖墙。
在阅读了一篇关于代表的文章后,我尝试制作自己的示例来帮助我理解 - > http://www.codeproject.com/Articles/71154/C-Delegates-101-A-Practical-Example 但到目前为止一直不成功。下面的代码产生关于这一行的错误:'doneExecuting = FunctionListToRun(MainOfWindows);' = 未分配的局部变量
有人可以告诉我我是否接近以及我做错了什么?
(我还在 pastebin 上包含了 UI 代码,以防它有帮助 -> http://pastebin.com/D2BVZJXc)
public partial class MainWindow : Window
{
public delegate bool FunctionToCall(MainWindow windowInstance);
public MainWindow()
{
InitializeComponent();
addMethodNames();
}
private void addMethodNames()
{
ListBoxItem lbi1 = new ListBoxItem();
lbi1.Content = "Introduction"; lbi1.Name = "get" + lbi1.Content;
listMethods.Items.Add(lbi1);
ListBoxItem lbi2 = new ListBoxItem();
lbi1.Content = "Greeting"; lbi2.Name = "get" + lbi2.Content;
listMethods.Items.Add(lbi2);
ListBoxItem lbi3 = new ListBoxItem();
lbi3.Content = "Story"; lbi3.Name = "get" + lbi3.Content;
listMethods.Items.Add(lbi3);
ListBoxItem lbi4 = new ListBoxItem();
lbi4.Content = "MyName"; lbi4.Name = "get" + lbi4.Content;
listMethods.Items.Add(lbi4);
ListBoxItem lbi6 = new ListBoxItem();
lbi6.Content = "Conclusion"; lbi6.Name = "get" + lbi6.Content;
listMethods.Items.Add(lbi6);
}
private void btnAddAction_Click(object sender, RoutedEventArgs e)
{
ListBoxItem lbi = (ListBoxItem)listMethods.Items[listMethods.SelectedIndex];
listMethods.Items.Remove(lbi);
listActions.Items.Add(lbi);
}
private void btnRemoveAction_Click(object sender, RoutedEventArgs e)
{
listActions.Items.RemoveAt(listActions.SelectedIndex);
}
private void btnRun_Click(object sender, RoutedEventArgs e)
{
bool doneExecuting = false;
FunctionToCall FunctionListToRun;
foreach (ListBoxItem methodName in listActions.Items)
{
Conclusion conc = new Conclusion();
switch (methodName.Content.ToString())
{
case "Introduction":
Introduction intro = new Introduction();
FunctionListToRun = intro.getIntroduction;
break;
case "Greeting":
Greeting greet = new Greeting();
FunctionListToRun = greet.getGreeting;
break;
case "Story":
Story story = new Story();
FunctionListToRun = story.getStory;
break;
case "MyName":
MyName name = new MyName();
FunctionListToRun = name.getName;
break;
case "Conclusion":
FunctionListToRun = conc.getConclusion;
break;
default:
FunctionListToRun = conc.getConclusion;
break;
}
}
doneExecuting = FunctionListToRun(MainOfWindows);
}
}
class Introduction
{
public bool getIntroduction(MainWindow windowInstance)
{
windowInstance.txtResult.Text += " Hello there!";
return true;
}
}
class Greeting
{
public bool getGreeting(MainWindow windowInstance)
{
windowInstance.txtResult.Text += " How are you today?";
return true;
}
}
class Story
{
public bool getStory(MainWindow windowInstance)
{
windowInstance.txtResult.Text += " I once met a goat and his name was billy, and he lived on a plain that was very hilly.";
return true;
}
}
class MyName
{
public bool getName(MainWindow windowInstance)
{
windowInstance.txtResult.Text += " My name is too infinity!";
return true;
}
}
class Conclusion
{
public bool getConclusion(MainWindow windowInstance)
{
windowInstance.txtResult.Text += " That is all, goodbye!";
return true;
}
}