[索引页]
[源码下载]
作者:webabcd
介绍
Silverlight 3.0 提示和技巧系列
GPU.xaml.cs
[源码下载]
稳扎稳打Silverlight(41) - 3.0Tip/Trick之GPU 加速,Out-Of-browser,应用程序库缓存,合并 ResourceDictionary,应用程序扩展服务,Silverlight 插件对象
作者:webabcd
介绍
Silverlight 3.0 提示和技巧系列
- GPU 加速 - 对 GPU 加速的支持
- Out-Of-browser - 浏览器外运行,即支持脱离浏览器运行
- 应用程序库缓存 - 将 dll(zip) 缓存到客户端浏览器中
- 合并 ResourceDictionary - 整合不同位置的 ResourceDictionary
- 应用程序扩展服务 - 通过扩展 Application 来提供附加服务
- Silverlight 插件对象的新增功能 - 在 Silverlight 3.0 中 Silverlight 插件对象的新增功能
<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">
<Grid x:Name="LayoutRoot">
<Grid.Resources>
<Storyboard x:Name="ani" AutoReverse="True" RepeatBehavior="Forever">
<DoubleAnimation Storyboard.TargetName="st" Storyboard.TargetProperty="ScaleX"
From="1" To="3" Duration="0:0:3" />
<DoubleAnimation Storyboard.TargetName="st" Storyboard.TargetProperty="ScaleY"
From="1" To="3" Duration="0:0:3" />
<DoubleAnimation Storyboard.TargetName="rt" Storyboard.TargetProperty="Angle"
From="0" To="360" Duration="0:0:3" />
<DoubleAnimation Storyboard.TargetName="mediaElement" Storyboard.TargetProperty="Opacity"
From="1" To="0.3" Duration="0:0:3" />
</Storyboard>
</Grid.Resources>
<MediaElement x:Name="mediaElement" Source="/Resource/Demo.mp4" Width="320" Height="240"
Autoplay="True" MediaEnded="mediaElement_MediaEnded" RenderTransformOrigin="0.5,0.5">
<MediaElement.RenderTransform>
<TransformGroup>
<ScaleTransform x:Name="st" ScaleX="1" ScaleY="1" />
<RotateTransform x:Name="rt" Angle="0" />
</TransformGroup>
</MediaElement.RenderTransform>
<!--
CacheMode - 缓存类型,用于 GPU 加速,当前只支持 BitmapCache(用 cpu 呈现 UIElement 一次,其结果位图会被缓存到 GPU, 然后通过 GPU 处理)
可被 GPU 加速的有: Scale,Rotate,Opacity,矩形的Clip
CacheMode.RenderAtScale - 位图缓存相对于当前呈现的 UIElement 的放大倍数。由于需要缓存为位图,而对于 Silverlight 的矢量图形来讲,则可以设置此属性以尽可能避免失真(当然,此值设置得越高,GPU 的负担也会越重)
-->
<MediaElement.CacheMode>
<BitmapCache RenderAtScale="1" />
</MediaElement.CacheMode>
</MediaElement>
<Image Source="/Resource/logo.jpg" Width="160" Height="120" />
</Grid>
</navigation:Page>
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">
<Grid x:Name="LayoutRoot">
<Grid.Resources>
<Storyboard x:Name="ani" AutoReverse="True" RepeatBehavior="Forever">
<DoubleAnimation Storyboard.TargetName="st" Storyboard.TargetProperty="ScaleX"
From="1" To="3" Duration="0:0:3" />
<DoubleAnimation Storyboard.TargetName="st" Storyboard.TargetProperty="ScaleY"
From="1" To="3" Duration="0:0:3" />
<DoubleAnimation Storyboard.TargetName="rt" Storyboard.TargetProperty="Angle"
From="0" To="360" Duration="0:0:3" />
<DoubleAnimation Storyboard.TargetName="mediaElement" Storyboard.TargetProperty="Opacity"
From="1" To="0.3" Duration="0:0:3" />
</Storyboard>
</Grid.Resources>
<MediaElement x:Name="mediaElement" Source="/Resource/Demo.mp4" Width="320" Height="240"
Autoplay="True" MediaEnded="mediaElement_MediaEnded" RenderTransformOrigin="0.5,0.5">
<MediaElement.RenderTransform>
<TransformGroup>
<ScaleTransform x:Name="st" ScaleX="1" ScaleY="1" />
<RotateTransform x:Name="rt" Angle="0" />
</TransformGroup>
</MediaElement.RenderTransform>
<!--
CacheMode - 缓存类型,用于 GPU 加速,当前只支持 BitmapCache(用 cpu 呈现 UIElement 一次,其结果位图会被缓存到 GPU, 然后通过 GPU 处理)
可被 GPU 加速的有: Scale,Rotate,Opacity,矩形的Clip
CacheMode.RenderAtScale - 位图缓存相对于当前呈现的 UIElement 的放大倍数。由于需要缓存为位图,而对于 Silverlight 的矢量图形来讲,则可以设置此属性以尽可能避免失真(当然,此值设置得越高,GPU 的负担也会越重)
-->
<MediaElement.CacheMode>
<BitmapCache RenderAtScale="1" />
</MediaElement.CacheMode>
</MediaElement>
<Image Source="/Resource/logo.jpg" Width="160" Height="120" />
</Grid>
</navigation:Page>
GPU.xaml.cs
GPUTestPage.html(宿主页)
<
div
id
="silverlightControlHost"
>
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2"
width="100%" height="100%">
< param name ="source" value ="ClientBin/Silverlight30.xap" />
< param name ="onError" value ="onSilverlightError" />
< param name ="background" value ="white" />
< param name ="minRuntimeVersion" value ="3.0.40624.0" />
< param name ="autoUpgrade" value ="true" />
< ! --enableGPUacceleration - 是否启用 GPU 加速-- >
< param name ="enableGPUacceleration" value ="true" />
< ! --enableCacheVisualization - 是否将缓存区域可视化-- >
<!--
如果该属性为 true 则:
本身的颜色代表被缓存的对象
红色代表未被缓存的对象
绿色代表被自动缓存的对象。如果某对象在被缓存对象的上面,那么该对象会被自动缓存
-->
< param name ="enableCacheVisualization" value ="true" />
< ! --enableFramerateCounter - 是否显示帧率的相关信息-- >
<!--
显示的信息在 Silverlight 程序的左上角以黑底白字做显示:
第一个参数:帧率(FPS)
第二个参数:GPU 的内存的使用情况。单位:KB
-->
< param name ="enableFramerateCounter" value ="true" />
< a href ="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style ="text-decoration: none" >
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight"
style="border-style: none" />
</a>
</object>
<iframe id="_sl_historyFrame" style="visibility: hidden; height: 0px; width: 0px;
border: 0px"> </iframe>
</div>
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2"
width="100%" height="100%">
< param name ="source" value ="ClientBin/Silverlight30.xap" />
< param name ="onError" value ="onSilverlightError" />
< param name ="background" value ="white" />
< param name ="minRuntimeVersion" value ="3.0.40624.0" />
< param name ="autoUpgrade" value ="true" />
< ! --enableGPUacceleration - 是否启用 GPU 加速-- >
< param name ="enableGPUacceleration" value ="true" />
< ! --enableCacheVisualization - 是否将缓存区域可视化-- >
<!--
如果该属性为 true 则:
本身的颜色代表被缓存的对象
红色代表未被缓存的对象
绿色代表被自动缓存的对象。如果某对象在被缓存对象的上面,那么该对象会被自动缓存
-->
< param name ="enableCacheVisualization" value ="true" />
< ! --enableFramerateCounter - 是否显示帧率的相关信息-- >
<!--
显示的信息在 Silverlight 程序的左上角以黑底白字做显示:
第一个参数:帧率(FPS)
第二个参数:GPU 的内存的使用情况。单位:KB
-->
< param name ="enableFramerateCounter" value ="true" />
< a href ="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style ="text-decoration: none" >
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight"
style="border-style: none" />
</a>
</object>
<iframe id="_sl_historyFrame" style="visibility: hidden; height: 0px; width: 0px;
border: 0px"> </iframe>
</div>
<navigation:Page x:Class="Silverlight30.Tip.OutOfbrowser"
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="OutOfbrowser Page">
<Grid x:Name="LayoutRoot">
<!--安装/卸载按钮-->
<Button x:Name="button" Click="button_Click" Width="100" Height="30" />
</Grid>
</navigation:Page>
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="OutOfbrowser Page">
<Grid x:Name="LayoutRoot">
<!--安装/卸载按钮-->
<Button x:Name="button" Click="button_Click" Width="100" Height="30" />
</Grid>
</navigation:Page>
OutOfbrowser.xaml.cs
3、应用程序库缓存的说明
ApplicationLibraryCaching.xaml
ApplicationLibraryCaching.xaml
<navigation:Page x:Class="Silverlight30.Tip.ApplicationLibraryCaching"
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="ApplicationLibraryCaching Page">
<Grid x:Name="LayoutRoot">
<TextBlock>
<Run>
应用程序缓存 - 将 dll 缓存到客户端浏览器中
</Run>
<LineBreak />
<LineBreak />
<Run>
启用“应用程序缓存” - 在 Silverlight 项目上单击右键 -> 属性 -> 勾选“ Reduce XAP size by using application library caching”复选框
</Run>
<LineBreak />
<Run>
“应用程序缓存”不能和“脱离浏览器支持”共同使用
</Run>
<LineBreak />
<Run>
启用应用程序缓存后,可被缓存的 dll 的属性 copy Local 会被设置为 true
</Run>
<LineBreak />
<Run>
启用应用程序缓存后,所有可被缓存的 dll 会单独生成一个 zip 包
</Run>
<LineBreak />
<Run>
AppManifest.xml 会增加相应的 ExternalParts。例: <Deployment.ExternalParts><ExtensionPart Source="System.Xml.Linq.zip" /></Deployment.ExternalParts>
</Run>
</TextBlock>
</Grid>
</navigation:Page>
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="ApplicationLibraryCaching Page">
<Grid x:Name="LayoutRoot">
<TextBlock>
<Run>
应用程序缓存 - 将 dll 缓存到客户端浏览器中
</Run>
<LineBreak />
<LineBreak />
<Run>
启用“应用程序缓存” - 在 Silverlight 项目上单击右键 -> 属性 -> 勾选“ Reduce XAP size by using application library caching”复选框
</Run>
<LineBreak />
<Run>
“应用程序缓存”不能和“脱离浏览器支持”共同使用
</Run>
<LineBreak />
<Run>
启用应用程序缓存后,可被缓存的 dll 的属性 copy Local 会被设置为 true
</Run>
<LineBreak />
<Run>
启用应用程序缓存后,所有可被缓存的 dll 会单独生成一个 zip 包
</Run>
<LineBreak />
<Run>
AppManifest.xml 会增加相应的 ExternalParts。例: <Deployment.ExternalParts><ExtensionPart Source="System.Xml.Linq.zip" /></Deployment.ExternalParts>
</Run>
</TextBlock>
</Grid>
</navigation:Page>
4、合并 ResourceDictionary 的演示
App.xaml(在此处合并不同位置的 ResourceDictionary)
App.xaml(在此处合并不同位置的 ResourceDictionary)
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Silverlight30.App"
>
<Application.Resources>
<!--
以 xaml 方式整合外部 ResourceDictionary
-->
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Style/ButtonStyle1.xaml" />
<ResourceDictionary Source="Style/ButtonStyle2.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<!--
以 cs 方式整合外部 ResourceDictionary
ResourceDictionary dict = System.Windows.Markup.XamlReader.Load(xaml) as ResourceDictionary;
Resources.MergedDictionaries.Add(dict);
-->
</Application.Resources>
</Application>
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Silverlight30.App"
>
<Application.Resources>
<!--
以 xaml 方式整合外部 ResourceDictionary
-->
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Style/ButtonStyle1.xaml" />
<ResourceDictionary Source="Style/ButtonStyle2.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<!--
以 cs 方式整合外部 ResourceDictionary
ResourceDictionary dict = System.Windows.Markup.XamlReader.Load(xaml) as ResourceDictionary;
Resources.MergedDictionaries.Add(dict);
-->
</Application.Resources>
</Application>
5、应用程序扩展服务的 Demo
App.xaml(注册服务)
App.xaml(注册服务)
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Silverlight30.App"
xmlns:svc="clr-namespace:Silverlight30.Tip"
>
<!--
以 xaml 方式注册扩展服务
-->
<Application.ApplicationLifetimeObjects>
<svc:MyExtensionService />
</Application.ApplicationLifetimeObjects>
<!--
以 cs 方式注册扩展服务(在 App 的构造函数中)
ApplicationLifetimeObjects.Add(new Silverlight30.Tip.MyExtensionService());
-->
</Application>
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Silverlight30.App"
xmlns:svc="clr-namespace:Silverlight30.Tip"
>
<!--
以 xaml 方式注册扩展服务
-->
<Application.ApplicationLifetimeObjects>
<svc:MyExtensionService />
</Application.ApplicationLifetimeObjects>
<!--
以 cs 方式注册扩展服务(在 App 的构造函数中)
ApplicationLifetimeObjects.Add(new Silverlight30.Tip.MyExtensionService());
-->
</Application>
MyExtensionService.cs
ApplicationExtensionServices.xaml.cs
<navigation:Page x:Class="Silverlight30.Tip.Plugin"
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="Plugin Page">
<Grid x:Name="LayoutRoot">
<TextBlock>
<Run>
AllowHtmlPopupWindow - 是否允许使用 HtmlPage.PopupWindow 弹出新窗口
</Run>
<LineBreak />
<Run>
EnabledAutoZoom - 是否自动调整大小以适应浏览器的缩放
</Run>
<LineBreak />
<Run>
Enablednavigation - 是否可以使用 HyperlinkButton 导航到外部链接
</Run>
<LineBreak />
<Run>
Application.Current.Host.Settings,Application.Current.Host.Content - 可对插件的一些属性和一些事件做设置
</Run>
<LineBreak />
<Run>
EnableGPUacceleration,EnableCacheVisualization,EnableFramerateCounter - 参见 GPUTestPage.html 中的说明
</Run>
</TextBlock>
</Grid>
</navigation:Page>
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="Plugin Page">
<Grid x:Name="LayoutRoot">
<TextBlock>
<Run>
AllowHtmlPopupWindow - 是否允许使用 HtmlPage.PopupWindow 弹出新窗口
</Run>
<LineBreak />
<Run>
EnabledAutoZoom - 是否自动调整大小以适应浏览器的缩放
</Run>
<LineBreak />
<Run>
Enablednavigation - 是否可以使用 HyperlinkButton 导航到外部链接
</Run>
<LineBreak />
<Run>
Application.Current.Host.Settings,Application.Current.Host.Content - 可对插件的一些属性和一些事件做设置
</Run>
<LineBreak />
<Run>
EnableGPUacceleration,EnableCacheVisualization,EnableFramerateCounter - 参见 GPUTestPage.html 中的说明
</Run>
</TextBlock>
</Grid>
</navigation:Page>
OK
[源码下载]
[源码下载]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。