WPF使用Delegate.CreateDelegate() 把方法名赋值给控件点击事件报错

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 }

2、错误信息为:

System.ArgumentException:“无法绑定到目标方法,因其签名或安全透明度与委托类型的签名或安全透明度不兼容

3、Delegate.CreateDelegate()微软官方解读:

https://docs.microsoft.com/zh-cn/dotnet/api/system.delegate.createdelegate?view=net-6.0#System_Delegate_CreateDelegate_System_Type_System_Object_System_String_

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     }
365个夜晚,我希望做到两天更一篇博客。加油,小白!
原文地址:https://www.cnblogs.com/qq2806933146xiaobai/p/15700256.html