1、出错代码:
1 public partial class MainWindow2 : Window
2 {
3
4 public MainWindow2()
5 {
6 InitializeComponent();
7 // 页面初始化
8 //OnLoad();
9 }
10 ....
11 public void funA(){
12
13 #region
14 List<Menus> menuList = new List<Menus>();
15 {
16 menuList.Add(new Menus()
17 {
18 ParentId = 2,
19 Name = "系统配置",
20 DisplayName = "系统配置",
21 LinkUrl = "BtnSetup_Click",
22 Permission = "1",
23 IsDisplay = true
24 });
25 menuList.Add(new Menus()
26 {
27 ParentId = 2,
28 Name = "扩展工具",
29 DisplayName = "扩展工具",
30 LinkUrl = "BtnExtension_Click",
31 Permission = "0",
32 IsDisplay = true
33 });
34 }
35 foreach (Menus menu in menuList)
36 {
37 ListBoxItem btnSetup = new ListBoxItem()
38 {
39 Content = menu.DisplayName,
40 Name = "btnSetup",
41 Visibility = menu.Permission == "1" ? Visibility.Visible : Visibility.Collapsed
42 };
43 EventInfo vEventInfo = typeof(ListBoxItem).GetEvent("MouseLeftButtonUp");
44 object target = this;
45 vEventInfo.AddEventHandler(btnSetup, Delegate.CreateDelegate(vEventInfo.EventHandlerType, target, menu.LinkUrl));
46 lstMore.Items.Add(btnSetup);
47 }
48 #endregion
49 }
50
51 public void BtnSetup_Click(object sender, RoutedEventArgs e)
52 {
53 MessageBox.Show("点击了系统配置按钮");
54 }
55 }
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
2、错误信息为:
3、Delegate.CreateDelegate()微软官方解读:
4、解决状态:
未解决
5、对应方案:
为控件绑定同一事件并建立tag标签,用case 'tag标签值(或者控件名)'来区分不同业务逻辑。
事件部分代码:
1 /// <summary>
2 /// 功能按钮
3 /// </summary>
4 public void BtnPage_Click(object sender, RoutedEventArgs e)
5 {
6 //string Tag;
7 //if (sender is Button)
8 //{
9 // Button button = sender as Button;
10 // Tag = button.Tag.ToString();
11 //}
12 //else if (sender is ListBoxItem)
13 //{
14
15 // ListBoxItem button = sender as ListBoxItem;
16 // Tag = button.Tag.ToString();
17 //}
18
19 System.Windows.Controls.Control control = sender as System.Windows.Controls.Control;
20
21 switch (control.Tag)
22 {
23 case "1":
24 MessageBox.Show("点击了系统配置按钮");
25 break;
26 case "2":
27 MessageBox.Show("点击了帮助按钮");
28 break;
29 }
30 }
31 }
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
作者:꧁执笔小白꧂