• Silverlight加载xap后通过反射相互调用方法及元素


    在一个silverlight工程里加载另一个xap之前我已经有写过,不过如果加载后相互调用就没有写到,所以补充说明一下加载后两个silverlight中相互调用方法和元素的使用。

    注意:请先参考本网站的《silverlight 动态加载xap》文章,再结合以下内容使用!

    1.在主程序里调用加载的xap里的方法:

    a.在被加载的silverlight里添加一个方法提供给调用:

    1:  public void test()
    2:  {
    3:      MessageBox.Show("hello Man");
    4:  }

    b.在主silverlight程序中调用上边的方法:

    1:  Type t = element.GetType();
    2:  MethodInfo m = t.GetMethod("test");
    3:  m.Invoke(element,null);

    这里请注意

    a)element是动态加载xpa后所得到的UIElement。(也就是被加载的silverlight,只是加载后会以一个UIElement形式返回)

    b) t.GetMethod("test"),这里的参数是被加载程序的方法名。

    c)Invoke方法重点有于Null参数,如果被调用的方法是有参数的,我们就可以通过这个参数去传递。

    2.在被加载程序中调用主程序的方法。

    a.在主程序中定义一个相同的方法供加载程序调用:

    1:  public void dothing()
    2:  {
    3:      MessageBox.Show("haha");
    4:  }

    b.在被调用程序中调用主的方法:

    1:  Type t = App.Current.RootVisual.GetType();
    2:  MethodInfo m = t.GetMethod("dothing");
    3:  m.Invoke(App.Current.RootVisual,null);

    a)这里有兴的是我们被加载后我们的App.Current不再是本程序的App.Current了。而是主程序的App.Current,而且RootVisual就是我们的MainPage,大家可以参考app.xaml.cs文件.由此我们可以对主程序为所欲为了。呵呵。

    b)通过 t.GetMethod("dothing");调用主程序中的方法。方法名为"dothing".

    c)m.Invoke调用方法并传入null参数,即为无参方法。

    3.如何调用有参数的方法:

    a.假设要调用的方法的参数定义如下:

    1:  public void dothing(int i,string s,double d)
    2:  {
    3:      MessageBox.Show("haha");
    4:  }

    b.我们可以通过以下方法传递参数:

    1:  Type t = App.Current.RootVisual.GetType();
    2:  MethodInfo m = t.GetMethod("dothing");
    3:  m.Invoke(App.Current.RootVisual,new object[3]{1,"string",2.2});

    这里请注意 m.Invoke的new object[3]{1,"string",2.2}这里三个值对应的就是被调用方法的dothing(int i,string s,double d)三个参数。反之在主程序中调有和加裁程序使用方法相同。

    4.如何取得xaml元素:

    假设在被加载程序中在如上的xaml元素定义:

    1:  <UserControl x:Class="SilverlightApplication1.MainPage"
    2:      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    3:      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4:      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    5:      mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
    6:    <Grid x:Name="LayoutRoot">
    7:  <Button x:Name="bt" Content="butt" ></Button>
    8:    </Grid>
    9:  </UserControl>

    假设我们需要取得上边所定义的button元素:

    a.在此button所有在cs文件上中定义一个方法。并且此方法的返回是一个button:

    1:  public Button getButton()
    2:  {
    3:      return bt;
    4:  }

    b.在主程序调用并取得元素:

    1:   Type t = element.GetType();
    2:   MethodInfo m = t.GetMethod("getButton");
    3:  Button takeButton = (Button)m.Invoke(element,null);

    如此即可取得被加载程序中的Button。

    5.在被加载程序中调用主程序的自定议属性:

    a.在主程序中定义属性:

    1:  private string haha;
    2:  
    3:  public string Haha
    4:  {
    5:      get { return haha; }
    6:      set { haha = value; }
    7:  }

    b.利害GetProperties方法取得所有属性:

    1:  Type t = App.Current.RootVisual.GetType();
    2:  PropertyInfo[] p = t.GetProperties();
    3:  var haha = p.First(h => h.Name.Equals("Haha"));
    4:  haha.GetSetMethod().Invoke(App.Current.RootVisual, new object[1] {"yoyo" });

    以上代码首先利用GetProperties方法取得所有主程序中的性性,然后通过lambda表达式取得名字为Haha的属性。(即主程序中定义的属性名称)最后通过取出的属性调用GetSetMethod方法调用Set方法,(相同原理你可以通过GetGetMethod方法去调用Get方法)。然后再调用Invoke方法指定属性所在的类和传入值即可。

  • 相关阅读:
    高速传输线PCB设计
    带状线和微带线
    资源分配
    异步时钟切换电路
    Mathcad操作tips:2D绘图
    Mathcad操作tips:函数、符号计算
    慢性胃炎注意事项
    Arduino I2C + 三轴加速度计ADXL345
    Arduino SPI + SPI Flash芯片W25Q80BV
    Arduino I2C + 三轴加速度计LIS3DH
  • 原文地址:https://www.cnblogs.com/jacle169/p/2810082.html
Copyright © 2020-2023  润新知