在一個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: <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方法指定屬性所在的類和傳入值即可。(原文出處:http://funsl.com)
結束語:
寫完這文章後有一個設想,可以通過這篇文章那寫的內容,開發一個sl的開發平台,讓使用者自己去開發sl項目。呵呵。。
REF:http://www.pin5i.com/showtopic-26082.html