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

来源:互联网 发布:计数器软件下载 编辑:程序博客网 时间:2024/06/08 11:21

在一个silverlight工程里加载另一个xap之前我已经有写过,不过如果加载后相互调用就没有写到,所以补充说明一下加载后两个silverlight中相互调用方法和元素的使用。
注意:请先参考本网站的《Silverlight3 加载其他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: 

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:   

7: 

8:   

9: 
复制代码

假设我们需要取得上边所定义的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方法指定属性所在的类和传入值即可。(原文出处:http://funsl.com)
结束语:
写完这文章后有一个设想,可以通过这篇文章那写的内容,开发一个sl的开发平台,让用户自己去开发sl项目。呵呵。。


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/dotfun/archive/2010/01/20/5217248.aspx

原创粉丝点击