您的位置:首页 > 移动开发 > Objective-C

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

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

注意:请先参考《Silverlight3 加载其他xap》文章,再结合以下内容使用!

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

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

public void test()
{
MessageBox.Show("hello Man");
  }

b.在主silverlight程序中调用上边的方法:
Type t = element.GetType();
 MethodInfo m = t.GetMethod("test");
m.Invoke(element,null);

这里请注意

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

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

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

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

a.在主程序中定义一个相同的方法供加载程序调用:
public void dothing()
{
MessageBox.Show("haha");
}b.在被调用程序中调用主的方法:
Type t = App.Current.RootVisual.GetType();

MethodInfo m = t.GetMethod("dothing");

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.假设要调用的方法的参数定义如下:
public void dothing(int i,string s,double d)
{
MessageBox.Show("haha");
}b.我们可以通过以下方法传递参数:
Type t = App.Current.RootVisual.GetType();
MethodInfo m = t.GetMethod("dothing");
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元素定义:
<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot">
<Button x:Name="bt" Content="butt" ></Button>
</Grid>
</UserControl>


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

a.在此button所有在cs文件上中定义一个方法。并且此方法的返回是一个button:
public Button getButton()
{
return bt;
}b.在主程序调用并取得元素:
Type t = element.GetType();
MethodInfo m = t.GetMethod("getButton");
Button takeButton = (Button)m.Invoke(element,null);如此即可取得被加载程序中的Button。

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

a.在主程序中定义属性:
private string haha;
public string Haha
{
get { return haha; }
set { haha = value; }
}

b.利害GetProperties方法取得所有属性:
Type t = App.Current.RootVisual.GetType();
PropertyInfo[] p = t.GetProperties();
var haha = p.First(h => h.Name.Equals("Haha"));
haha.GetSetMethod().Invoke(App.Current.RootVisual, new object[1] {"yoyo" });       以上代码首先利用GetProperties方法取得所有主程序中的性性,然后通过lambda表达式取得名字为Haha的属性。(即主程序中定义的属性名称)最后通过取出的属性调用GetSetMethod方法调用Set方法,(相同原理你可以通过GetGetMethod方法去调用Get方法)。然后再调用Invoke方法指定属性所在的类和传入值即可。
原文:http://www.pin5i.com/showtopic-26082.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息