zoukankan      html  css  js  c++  java
  • 实现Button的动态响应

    按下不同的Button实现不同的逻辑

    但用同样的代码:

     1 using System.Reflection;
     2 
     3 namespace valuableBook
     4 {
     5     /// <summary>
     6     /// Interaction logic for Item.xaml
     7     /// </summary>
     8     public partial class Item : Window
     9     {
    10         public Item()
    11         {
    12             InitializeComponent();
    13         }
    14 
    15         private void Button_Click(object sender, RoutedEventArgs e)
    16         {
    17             Button cmd = e.OriginalSource as Button;
    18             Type type = this.GetType();
    19             Assembly assembly = type.Assembly;
    20             Window win = (Window)assembly.CreateInstance(type.Namespace + "." + cmd.Tag);
    21             win.ShowDialog();
    22 
    23         }
    24     }
    25 }

    前台的Button

    1    <StackPanel Margin="5" Button.Click="Button_Click">
    2             <Button Content="MainWindow" Tag="MainWindow"></Button>
    3         </StackPanel>

    Msdn上的例子:

     1 using System;
     2 using System.Reflection;
     3 using Contoso.Libraries;
     4 
     5 namespace Contoso.Libraries
     6 {
     7    public class Person
     8    {
     9       private string _name;
    10 
    11       public Person()
    12       { }
    13 
    14       public Person(string name)
    15       {
    16          this._name = name;
    17       }
    18 
    19       public string Name
    20       { get { return this._name; }
    21         set { this._name = value; } }
    22 
    23       public override string ToString()
    24       {
    25          return this._name;
    26       }
    27    }
    28 }
    29 
    30 public class Example
    31 {
    32    public static void Main()
    33    {
    34       Assembly assem = typeof(Person).Assembly;
    35       Person p = (Person) assem.CreateInstance("Contoso.Libraries.Person");
    36       if (! (p == null)) {
    37          p.Name = "John";
    38          Console.WriteLine("Instantiated a {0} object whose value is '{1}'",
    39                            p.GetType().Name, p);
    40       }
    41       else {
    42          Console.WriteLine("Unable to instantiate a Person object.");
    43       }   
    44    }
    45 }
    46 // The example displays the following output: 
    47 //        Instantiated a Person object whose value is 'John'
  • 相关阅读:
    最简洁的Mysql修改密码,不会你打我
    两步解决jetbrains-agent-latest.zip不能拖入工作区
    JSON学习笔记
    【书评】【不推荐】《机器学习实战——基于Scikit-Learn和TensorFlow》
    weblogic更新部署
    功能网址
    jupyter快捷键
    MYSQL连接字符串参数说明
    C# 格式验证
    Supervisor
  • 原文地址:https://www.cnblogs.com/ants_double/p/5582464.html
Copyright © 2011-2022 走看看