天天看點

穩紮穩打Silverlight(41) - 3.0Tip/Trick之GPU 加速, Out-Of-Browser, 應用程式庫緩存

<a href="http://webabcd.blog.51cto.com/1787395/342743" target="_blank">[索引頁]</a>

<a href="http://down.51cto.com/data/100302">[源碼下載下傳]</a>

穩紮穩打Silverlight(41) - 3.0Tip/Trick之GPU 加速, Out-Of-Browser, 應用程式庫緩存, 合并 ResourceDictionary, 應用程式擴充服務, Silverlight 插件對象

介紹

Silverlight 3.0 提示和技巧系列

GPU 加速 - 對 GPU 加速的支援

Out-Of-Browser -  浏覽器外運作,即支援脫離浏覽器運作

應用程式庫緩存 - 将 dll(zip) 緩存到用戶端浏覽器中 

合并 ResourceDictionary - 整合不同位置的 ResourceDictionary 

應用程式擴充服務 - 通過擴充 Application 來提供附加服務

Silverlight 插件對象的新增功能 - 在 Silverlight 3.0 中 Silverlight 插件對象的新增功能

線上DEMO

示例

1、如何實作 GPU 加速

GPU.xaml

&lt;navigation:Page x:Class="Silverlight30.Tip.GPU"    

                     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" 

                     xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 

                     d:DesignWidth="640" d:DesignHeight="480" 

                     Title="GPU Page"&gt; 

        &lt;Grid x:Name="LayoutRoot"&gt; 

                &lt;Grid.Resources&gt; 

                        &lt;Storyboard x:Name="ani" AutoReverse="True" RepeatBehavior="Forever"&gt; 

                                &lt;DoubleAnimation Storyboard.TargetName="st" Storyboard.TargetProperty="ScaleX" 

                                        From="1" To="3" Duration="0:0:3" /&gt; 

                                &lt;DoubleAnimation Storyboard.TargetName="st" Storyboard.TargetProperty="ScaleY" 

                                &lt;DoubleAnimation Storyboard.TargetName="rt" Storyboard.TargetProperty="Angle" 

                                        From="0" To="360" Duration="0:0:3" /&gt; 

                                &lt;DoubleAnimation Storyboard.TargetName="mediaElement" Storyboard.TargetProperty="Opacity" 

                                        From="1" To="0.3" Duration="0:0:3" /&gt; 

                        &lt;/Storyboard&gt; 

                &lt;/Grid.Resources&gt; 

                &lt;MediaElement x:Name="mediaElement" Source="/Resource/Demo.mp4" Width="320" Height="240" 

                        AutoPlay="True" MediaEnded="mediaElement_MediaEnded" RenderTransformOrigin="0.5,0.5"&gt; 

                        &lt;MediaElement.RenderTransform&gt; 

                                &lt;TransformGroup&gt; 

                                        &lt;ScaleTransform x:Name="st" ScaleX="1" ScaleY="1" /&gt; 

                                        &lt;RotateTransform x:Name="rt" Angle="0" /&gt; 

                                &lt;/TransformGroup&gt; 

                        &lt;/MediaElement.RenderTransform&gt; 

                        &lt;!-- 

                                CacheMode - 緩存類型,用于 GPU 加速,目前隻支援 BitmapCache(用 CPU 呈現 UIElement 一次,其結果位圖會被緩存到 GPU, 然後通過 GPU 處理) 

                                        可被 GPU 加速的有: Scale, Rotate, Opacity, 矩形的Clip 

                                CacheMode.RenderAtScale - 位圖緩存相對于目前呈現的 UIElement 的放大倍數。由于需要緩存為位圖,而對于 Silverlight 的矢量圖形來講,則可以設定此屬性以盡可能避免失真(當然,此值設定得越高,GPU 的負擔也會越重) 

                        --&gt; 

                        &lt;MediaElement.CacheMode&gt; 

                                &lt;BitmapCache RenderAtScale="1" /&gt; 

                        &lt;/MediaElement.CacheMode&gt; 

                &lt;/MediaElement&gt; 

                &lt;Image Source="/Resource/Logo.jpg" Width="160" Height="120" /&gt; 

        &lt;/Grid&gt; 

&lt;/navigation:Page&gt;

GPU.xaml.cs

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Net; 

using System.Windows; 

using System.Windows.Controls; 

using System.Windows.Documents; 

using System.Windows.Input; 

using System.Windows.Media; 

using System.Windows.Media.Animation; 

using System.Windows.Shapes; 

using System.Windows.Navigation; 

namespace Silverlight30.Tip 

        public partial class GPU : Page 

        { 

                public GPU() 

                { 

                        InitializeComponent(); 

                        this.Loaded += new RoutedEventHandler(GPU_Loaded); 

                } 

                void GPU_Loaded(object sender, RoutedEventArgs e) 

                        ani.Begin(); 

                private void mediaElement_MediaEnded(object sender, RoutedEventArgs e) 

                        // 重播 

                        mediaElement.Stop(); 

                        mediaElement.Play(); 

        } 

}

GPUTestPage.html(宿首頁)

&lt;div id="silverlightControlHost"&gt; 

                &lt;object data="data:application/x-silverlight-2," type="application/x-silverlight-2" 

                        width="100%" height="100%"&gt; 

                        &lt;param name="source" value="ClientBin/Silverlight30.xap" /&gt; 

                        &lt;param name="onError" value="onSilverlightError" /&gt; 

                        &lt;param name="background" value="white" /&gt; 

                        &lt;param name="minRuntimeVersion" value="3.0.40624.0" /&gt; 

                        &lt;param name="autoUpgrade" value="true" /&gt; 

                        &lt;!--enableGPUAcceleration - 是否啟用 GPU 加速--&gt; 

                        &lt;param name="enableGPUAcceleration" value="true" /&gt; 

                        &lt;!--enableCacheVisualization - 是否将緩存區域可視化--&gt; 

                            如果該屬性為 true 則: 

                                    本身的顔色代表被緩存的對象 

                                    紅色代表未被緩存的對象 

                                    綠色代表被自動緩存的對象。如果某對象在被緩存對象的上面,那麼該對象會被自動緩存 

                    --&gt; 

                        &lt;param name="enableCacheVisualization" value="true" /&gt; 

                        &lt;!--enableFramerateCounter - 是否顯示幀率的相關資訊--&gt; 

                            顯示的資訊在 Silverlight 程式的左上角以黑底白字做顯示: 

                                    第一個參數:幀率(FPS) 

                                    第二個參數:GPU 的記憶體的使用情況。機關:KB 

                        &lt;param name="enableFramerateCounter" value="true" /&gt; 

                        &lt;a href="http://go.microsoft.com/fwlink/?LinkID=149156&amp;v=3.0.40624.0" style="text-decoration: none"&gt; 

                                &lt;img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" 

                                        style="border-style: none" /&gt; 

                        &lt;/a&gt; 

                &lt;/object&gt; 

                &lt;iframe id="_sl_historyFrame" style="visibility: hidden; height: 0px; width: 0px; 

                        border: 0px"&gt;&lt;/iframe&gt; 

        &lt;/div&gt;

2、Out-Of-Browser(對浏覽器外運作的支援)

OutOfBrowser.xaml

&lt;navigation:Page x:Class="Silverlight30.Tip.OutOfBrowser"    

                     Title="OutOfBrowser Page"&gt; 

                &lt;!--安裝/解除安裝按鈕--&gt;         

                &lt;Button x:Name="button" Click="button_Click" Width="100" Height="30" /&gt; 

OutOfBrowser.xaml.cs

/* 

 * Application.InstallStateChanged - 浏覽器外運作的相關狀态發生改變時所觸發的事件 

 * Application.InstallState - 浏覽器外運作的相關狀态 [System.Windows.InstallState 枚舉] 

 *         NotInstalled - 在浏覽器中運作 

 *         Installing - 安裝到桌面中 

 *         Installed - 在浏覽器外運作 

 *         InstallFailed - 安裝到桌面的過程中發生錯誤 

 * Application.IsRunningOutOfBrowser - 目前程式是否是從浏覽器外啟動的 

 * Application.Install() - 安裝 Silverlight 程式到浏覽器外(解除安裝隻能通過右鍵菜單的方式解除安裝) 

 * Application.CheckAndDownloadUpdateAsync, Application.CheckAndDownloadUpdateCompleted - 一對異步方法/事件,用于更新浏覽器外運作的 Silverlight 程式(從伺服器上下載下傳新的版本) 

 *    

 * 注:在 Silverlight 項目上單擊右鍵 -&gt; 屬性 -&gt; Out-of-Browser Settings 可以對“浏覽器外運作”的相關參數做設定(也可以手動修改 Properties/OutOfBrowserSettings.xml) 

 */ 

        public partial class OutOfBrowser : Page 

                public OutOfBrowser() 

                        this.Loaded += new RoutedEventHandler(OutOfBrowser_Loaded); 

                void OutOfBrowser_Loaded(object sender, RoutedEventArgs e) 

                        InitButton(); 

                        App.Current.InstallStateChanged += new EventHandler(Current_InstallStateChanged); 

                void Current_InstallStateChanged(object sender, EventArgs e) 

                private void InitButton() 

                        if (App.Current.IsRunningOutOfBrowser) 

                        { 

                                button.Content = "解除安裝"; 

                        } 

                        else 

                                button.Content = "安裝"; 

                private void button_Click(object sender, RoutedEventArgs e) 

                        if (!App.Current.IsRunningOutOfBrowser &amp;&amp; App.Current.InstallState == InstallState.NotInstalled) 

                                App.Current.Install(); 

                                MessageBox.Show("請右鍵解除安裝"); 

3、應用程式庫緩存的說明

ApplicationLibraryCaching.xaml

&lt;navigation:Page x:Class="Silverlight30.Tip.ApplicationLibraryCaching"    

                     Title="ApplicationLibraryCaching Page"&gt; 

                &lt;TextBlock&gt; 

                        &lt;Run&gt; 

                                應用程式緩存 - 将 dll 緩存到用戶端浏覽器中 

                        &lt;/Run&gt; 

                        &lt;LineBreak /&gt; 

                                啟用“應用程式緩存” - 在 Silverlight 項目上單擊右鍵 -&gt; 屬性 -&gt; 勾選“ Reduce XAP size by using application library caching”複選框 

                                “應用程式緩存”不能和“脫離浏覽器支援”共同使用 

                                啟用應用程式緩存後,可被緩存的 dll 的屬性 Copy Local 會被設定為 true 

                                啟用應用程式緩存後,所有可被緩存的 dll 會單獨生成一個 zip 包 

                                AppManifest.xml 會增加相應的 ExternalParts。例: &lt;Deployment.ExternalParts&gt;&lt;ExtensionPart Source="System.Xml.Linq.zip" /&gt;&lt;/Deployment.ExternalParts&gt; 

                &lt;/TextBlock&gt; 

4、合并 ResourceDictionary 的示範

App.xaml(在此處合并不同位置的 ResourceDictionary)

&lt;Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 

                         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    

                         x:Class="Silverlight30.App" 

&gt; 

        &lt;Application.Resources&gt; 

                &lt;!-- 

                        以 xaml 方式整合外部 ResourceDictionary 

                --&gt; 

                &lt;ResourceDictionary&gt; 

                        &lt;ResourceDictionary.MergedDictionaries&gt; 

                                &lt;ResourceDictionary Source="Style/ButtonStyle1.xaml" /&gt; 

                                &lt;ResourceDictionary Source="Style/ButtonStyle2.xaml" /&gt; 

                        &lt;/ResourceDictionary.MergedDictionaries&gt; 

                &lt;/ResourceDictionary&gt; 

                        以 cs 方式整合外部 ResourceDictionary 

                        ResourceDictionary dict = System.Windows.Markup.XamlReader.Load(xaml) as ResourceDictionary;    

                        Resources.MergedDictionaries.Add(dict); 

        &lt;/Application.Resources&gt; 

&lt;/Application&gt;

5、應用程式擴充服務的 Demo

App.xaml(注冊服務)

                         xmlns:svc="clr-namespace:Silverlight30.Tip" 

        &lt;!-- 

                以 xaml 方式注冊擴充服務 

        --&gt; 

        &lt;Application.ApplicationLifetimeObjects&gt; 

                &lt;svc:MyExtensionService /&gt; 

        &lt;/Application.ApplicationLifetimeObjects&gt; 

                以 cs 方式注冊擴充服務(在 App 的構造函數中) 

                ApplicationLifetimeObjects.Add(new Silverlight30.Tip.MyExtensionService()); 

MyExtensionService.cs

 * 自定義擴充服務需要實作 IApplicationService 接口或 IApplicationLifetimeAware 接口 

 * IApplicationService 接口需要實作 StartService() 方法(Application.Startup 事件之前調用)和 StopService() 方法(Application.Exit 事件之後調用) 

 * IApplicationLifetimeAware 接口需要實作的方法有 Starting(), Started(), Exiting(), Exited() 其分别在 Application.Startup 事件的之前和之後調用,以及 Application.Exit 事件的之前和之後調用 

using System.Windows.Ink; 

        public class MyExtensionService : IApplicationService 

                public static MyExtensionService Current { get; set; } 

                public string ExtensionInfo { get; set; } 

                public void StartService(ApplicationServiceContext context) 

                        Current = this; 

                        ExtensionInfo = "abc xyz"; 

                public void StopService() 

ApplicationExtensionServices.xaml.cs

        public partial class ApplicationExtensionServices : Page 

                public ApplicationExtensionServices() 

                        this.Loaded += new RoutedEventHandler(ApplicationExtensionServices_Loaded); 

                void ApplicationExtensionServices_Loaded(object sender, RoutedEventArgs e) 

                        // 調用自定義的擴充服務 

                        MessageBox.Show(MyExtensionService.Current.ExtensionInfo); 

6、Silverlight 插件對象的新增功能的簡要說明

Plugin.xaml

&lt;navigation:Page x:Class="Silverlight30.Tip.Plugin"    

                     Title="Plugin Page"&gt; 

                                AllowHtmlPopupWindow - 是否允許使用 HtmlPage.PopupWindow 彈出新視窗 

                                EnabledAutoZoom - 是否自動調整大小以适應浏覽器的縮放 

                                EnabledNavigation - 是否可以使用 HyperlinkButton 導航到外部連結 

                                Application.Current.Host.Settings, Application.Current.Host.Content - 可對插件的一些屬性和一些事件做設定 

                                EnableGPUAcceleration, EnableCacheVisualization, EnableFramerateCounter - 參見 GPUTestPage.html 中的說明 

OK

<a href="http://down.51cto.com/data/100302" target="_blank">[源碼下載下傳]</a>

          本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/342772,如需轉載請自行聯系原作者

繼續閱讀