专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Silverlight

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

发布时间:2011-06-27 20:16:12 文章来源:www.iduyao.cn 采编人员:星星草
Silverlight加载xap后通过反射相互调用方法及元素
在一个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" }); 
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: