|
概述
眾所周知,在Silverlight 2開始每個(gè)項(xiàng)目編譯后都會(huì)打包成為一個(gè)xap文件,如果我們要訪問當(dāng)前xap文件中的UserControl比較容易,那我們?nèi)绾卧L問一個(gè)外部xap文件中的內(nèi)容呢?甚至于如何訪問一個(gè)互聯(lián)網(wǎng)上的xap文件呢?
本文將簡(jiǎn)單介紹一下在Silverlight中如何訪問外部xap文件。
需求
現(xiàn)在我們先來(lái)看一下需求,大致是這樣子的,在服務(wù)端我們有兩個(gè)xap文件,其中MainProject.xap文件將會(huì)在MainProjectTestPage.ASPx中引用,而ExternalProject.xap文件中的UserControl將會(huì)在MainProject.xap文件中訪問,并進(jìn)行顯示,如下圖所示:
現(xiàn)在我們來(lái)建立相關(guān)的項(xiàng)目,最終完成的項(xiàng)目結(jié)構(gòu)如下圖所示:
這樣在編譯后,將會(huì)在ClientBin文件夾下產(chǎn)生兩個(gè).xap文件,現(xiàn)在我們將在MainProject.xap文件中訪問ExternalProject.xap中的UserControl。
分析
在實(shí)現(xiàn)這個(gè)過程中,我們將會(huì)遇到兩個(gè)問題:
1.因?yàn)闆]有任何頁(yè)面引用ExternalProject.xap文件,所以它不會(huì)下載到客戶端,這一點(diǎn)我們可以通過編碼的方式來(lái)下載它。
2.訪問ExternalProject.xap中的UserControl,我們需要找到對(duì)應(yīng)的程序集,以便使用反射,我們知道在xap文件是一個(gè)標(biāo)準(zhǔn)的zip文件,它會(huì)包含相關(guān)的程序集(接下來(lái)我會(huì)寫一篇文章專門解釋xap文件),如下圖所示:
現(xiàn)在解決了xap文件的下載已經(jīng)程序集的訪問問題,我們可以著手來(lái)實(shí)現(xiàn)了。
實(shí)現(xiàn)
實(shí)現(xiàn)的過程也是相當(dāng)簡(jiǎn)單,首先我們使用WebClient去下載xap文件,相信大家都知道該怎么做了,如下代碼所示
void myButton_Click(object sender, RoutedEventArgs e){ Uri address = new Uri("http://localhost:4161/ClientBin/ExternalProject.xap"); WebClient webClient = new WebClient(); webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted); webClient.OpenReadAsync(address);}void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e){ // 得到下載結(jié)果}
這一步比較簡(jiǎn)單,接下來(lái)我們將根據(jù)下載的結(jié)果,得到相應(yīng)的程序集。我們知道在xap文件中的AppManifest.xaml文件相當(dāng)于一個(gè)清單,列出了當(dāng)前xap文件用到的程序集(下篇文章將會(huì)介紹),它的內(nèi)容如下所示:
<Deployment xmlns="http://schemas.microsoft.com/client/2007/deployment" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" EntryPointAssembly="ExternalProject" EntryPointType="ExternalProject.App" RuntimeVersion="2.0.30523.6"> <Deployment.Parts> <AssemblyPart x:Name="ExternalProject" Source="ExternalProject.dll" /> </Deployment.Parts></Deployment>
注意,在Deployment.Parts節(jié)點(diǎn)下包含了當(dāng)前應(yīng)用程序中所有的程序集。首先要根據(jù)下載的結(jié)果獲取AppManifest.xaml文件中的內(nèi)容,如下代碼所示:
Stream stream = Application.GetResourceStream( new StreamResourceInfo(packageStream, null), new Uri("AppManifest.xaml", UriKind.Relative)).Stream;String appManifestString = new StreamReader(stream).ReadToEnd();
有了AppManifest.xaml中內(nèi)容,就可以根據(jù)它來(lái)構(gòu)造一個(gè)Deployment對(duì)象,Deployment對(duì)象提供了當(dāng)前應(yīng)用程序的Part和本地化信息清單,它的定義如下所示:
注意它定義了一個(gè)很重要的屬性Parts,通過該屬性我們就可以訪問所有Deployment中的程序集。好了,現(xiàn)在我們看如何通過AppManifest.xaml中的內(nèi)容構(gòu)造Deployment對(duì)象,以及遍歷其中的程序集,如下代碼所示:
Deployment deployment = (Deployment)XamlReader.Load(appManifestString);Assembly assembly = null;foreach (AssemblyPart assemblyPart in deployment.Parts){ if (assemblyPart.Source == assemblyName) { String source = assemblyPart.Source; StreamResourceInfo streamInfo = Application.GetResourceStream( new StreamResourceInfo(packageStream, "application/binary"), new Uri(source,UriKind.Relative)); assembly = assemblyPart.Load(streamInfo.Stream); break; }}return assembly;
注意,在遍歷時(shí)如果我們找到程序集名等于我們想要訪問的程序集,則直接返回該程序集。最終完整的LoadAssemblyFromXap方法代碼如下:
Assembly LoadAssemblyFromXap(Stream packageStream,String assemblyName){ Stream stream = Application.GetResourceStream( new StreamResourceInfo(packageStream, null), new Uri("AppManifest.xaml", UriKind.Relative)).Stream; String appManifestString = new StreamReader(stream).ReadToEnd(); Deployment deployment = (Deployment)XamlReader.Load(appManifestString); Assembly assembly = null; foreach (AssemblyPart assemblyPart in deployment.Parts) { if (assemblyPart.Source == assemblyName) { String source = assemblyPart.Source; StreamResourceInfo streamInfo = Application.GetResourceStream( new StreamResourceInfo(packageStream, "application/binary"), new Uri(source,UriKind.Relative)); assembly = assemblyPart.Load(streamInfo.Stream); break; } } return assembly;}
得到程序集后,再使用反射創(chuàng)建相關(guān)的實(shí)例,并在頁(yè)面上加載,如下代碼所示:
Assembly assembly = LoadAssemblyFromXap(e.Result, "ExternalProject.dll");UIElement element = assembly.CreateInstance("ExternalProject.SubPage") as UIElement;this.holder.Children.Add(element);
運(yùn)行后效果如下圖所示:
跨域訪問
在上面的示例中,不涉及到跨域(我會(huì)專門寫一篇文章介紹)調(diào)用的問題,如果大家想訪問的xap文件與當(dāng)前xap文件不在同一站點(diǎn)中,需要添加跨域訪問文件,如下代碼所示:
clientaccesspolicy.xml:
<?xml version="1.0" encoding="utf-8"?><access-policy> <cross-domain-access> <policy> <allow-from http-request-headers="*" /> <domain uri="*"/> </allow-from> <grant-to> <resource path="/" include-subpaths="true"/> </grant-to> </policy> </cross-domain-access></access-policy>
總結(jié)
本文介紹了在Silverlight中如何訪問外部xap文件這一技巧,希望對(duì)大家有所幫助。示例代碼下載:
NET技術(shù):在Silverlight中如何訪問外部xap文件中UserControl,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。