大佬教程收集整理的这篇文章主要介绍了使用Silverlight Toolkit 应用以及动态切换主题,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
本教程将向您介绍如何让Silverlight应用程序应用主题,并允许用户动态切换。
(原文:http://weblogs.asp.net/lduveau/archive/2010/05/31/dynamically-apply-and-change-theme-with-the-silverlight-toolkit.aspx)
从Silverlight支持主题开始就能实现,但2010年4月以后的版本和Silverlight 4中的新特性,使得应用这些主题更加容易。
您需要知道:
截至2010年4月从工具包中可用的主题:
You Could easily create your own,the XAML files used by those themes can be found on your local folder after installing the toolkit,for the April 2010 version the path is:这些主题所使用的XAML文件,您可以轻松地创建的,安装工具包后在您的本地文件夹可以找到,2010年4月版本的路径是:
C:\Program Files\Microsoft SDKs\Silverlight\v4.0\Toolkit\Apr10\Themes\Xaml
如果没有它,安装最新版本的Silverlight工具包:
http://silverlight.codeplex.com/http://silverlight.codeplex.com/
然后打开Visual studio 2010,创建新的Silverlight导航应用程序(或Silverlight商业应用程序)。
右键单击Silverlight项目,选择“添加引用...”,然后选择NET选项卡中所有System.Windows.Controls.Toolkit .*的DLL。
安装该工具包后,VS工具箱中会有一个主题控件,在@H_145_20@mainPage.xaml页面Frame外面添加一个主题控件,在这里,只想将主题应用到页面的内容部分。然后设置ThemeUri属性为其中引用的样式:
<toolkit:Theme x:Name="ThemeContainer" ThemeUri="/System.Windows.Controls.Theming.bubbleCreme;component/Theme.xaml"> <toolkit:ContextMenuservice.ContextMenu> <toolkit:ContextMenu> <toolkit:MenuItem Header="Theme" IsEnabled="false"/> <toolkit:Separator /> <toolkit:MenuItem Header="Default" /> <toolkit:MenuItem Header="Bubble Creme" Command="{Staticresource themeCommanD}" CommandParameter="BubbleCreme"/> <toolkit:MenuItem Header="Bureau Black" Command="{Staticresource themeCommanD}" CommandParameter="BureauBlack"/> <toolkit:MenuItem Header="Bureau Blue" Command="{Staticresource themeCommanD}" CommandParameter="BureauBlue"/> <toolkit:MenuItem Header="Expression Dark" Command="{Staticresource themeCommanD}" CommandParameter="ExpressionDark"/> <toolkit:MenuItem Header="Expression Light" Command="{Staticresource themeCommanD}" CommandParameter="ExpressionLight"/> <toolkit:MenuItem Header="Rainier Orange" Command="{Staticresource themeCommanD}" CommandParameter="RainierOrange"/> <toolkit:MenuItem Header="Rainier Purple" Command="{Staticresource themeCommanD}" CommandParameter="RainierPurple"/> <toolkit:MenuItem Header="Shiny Blue" Command="{Staticresource themeCommanD}" CommandParameter="ShinyBlue"/> <toolkit:MenuItem Header="Shiny Red" Command="{Staticresource themeCommanD}" CommandParameter="ShinyRed"/> <toolkit:MenuItem Header="Whistler Blue" Command="{Staticresource themeCommanD}" CommandParameter="WhistlerBlue"/> </toolkit:ContextMenu> </toolkit:ContextMenuservice.ContextMenu>
如果你从工具箱拖放主题控件,Visual studio会自动添加“工具包”的命名空间前缀,如果没有,需要在页面标题中手动添加:xmlns:toolkit="http://scheR_283_11845@as.microsoft.com/winfx/2006/xaml/presentation/toolkit"
在Home Page(/Views/Home.xaml)添加几个控件(TextBox,Button,Calendar,…),并在浏览器中测试您的应用程序,您应该看到在内容部分应用的主题:
这就是,很容易。
添加一个上下文菜单列出可用的主题
在主题控件内的ContextMenuservice添加一个ContextMenu控件(在运行时右键会出现):
<toolkit:ContextMenuservice.ContextMenu> <toolkit:ContextMenu> <toolkit:MenuItem Header="Theme" IsEnabled="false"/> <toolkit:Separator /> <toolkit:MenuItem Header="Default" /> <toolkit:MenuItem Header="Bubble Creme" Command="{Staticresource themeCommanD}" CommandParameter="BubbleCreme"/> <toolkit:MenuItem Header="Bureau Black" Command="{Staticresource themeCommanD}" CommandParameter="BureauBlack"/> <toolkit:MenuItem Header="Bureau Blue" Command="{Staticresource themeCommanD}" CommandParameter="BureauBlue"/> <toolkit:MenuItem Header="Expression Dark" Command="{Staticresource themeCommanD}" CommandParameter="ExpressionDark"/> <toolkit:MenuItem Header="Expression Light" Command="{Staticresource themeCommanD}" CommandParameter="ExpressionLight"/> <toolkit:MenuItem Header="Rainier Orange" Command="{Staticresource themeCommanD}" CommandParameter="RainierOrange"/> <toolkit:MenuItem Header="Rainier Purple" Command="{Staticresource themeCommanD}" CommandParameter="RainierPurple"/> <toolkit:MenuItem Header="Shiny Blue" Command="{Staticresource themeCommanD}" CommandParameter="ShinyBlue"/> <toolkit:MenuItem Header="Shiny Red" Command="{Staticresource themeCommanD}" CommandParameter="ShinyRed"/> <toolkit:MenuItem Header="Whistler Blue" Command="{Staticresource themeCommanD}" CommandParameter="WhistlerBlue"/> </toolkit:ContextMenu> </toolkit:ContextMenuservice.ContextMenu>您需要添加一个引用这个DLL: System.Windows.Input.ToolkitSystem.Windows.Input.Toolkit
请注意,每个MenuItem被映射到一个命令,并且主题名称作为参数传递。
你必须创建一个新类,并实现ICommand接口。这里的目标是获得一个主题控件的引用(按名称,但您可能会找到更好的办法做到这一点),并设置其ThemeUri属性。
using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Windows.Markup; using System.IO; using System.Windows.Controls.Theming; namespace FunWithThemes { public class ThemeChangeCommand : ICommand { #region ICommand Members public bool CanExecute(object parameter) { return true; } public event EventHandler CanExecuteChanged; public void Execute(object parameter) { Theme themeContainer = (ThemE)((FrameworkElement)Application.Current.RootVisual).FindName("ThemeContainer"); String themename = parameter as String; if (themename == null) { themeContainer.ThemeUri = null; } else { themeContainer.ThemeUri = new Uri("/System.Windows.Controls.Theming." + themename + ";component/Theme.xaml",UriKind.RelativeOrAbsolutE); } if (CanExecuteChanged != null) CanExecuteChanged(this,new EventArgs()); } #endregion } }
如果使用“Silverlight的商业应用程序”模板,你应该改变类似 :
Theme themeContainer = (ThemE)((FrameworkElement)((ContentControl)Application.Current.RootVisual).Content).FindName( @H_873_301@"ThemeContainer" ); 在MainPage.xaml中(或在应用程序资源)的资源中添加命令:< Grid.resources >
< local:ThemeChangeCommand x:Key ="themeCommand" />
</ Grid.resources >"local"是应用程序的命名空间前缀 。
测试!
选择主题“Shiny Red”:
或 “Bureau Blue”:
您有多种存储策略(独立存储,服务器配置文件,甚至Cookies,... ...)
以上是大佬教程为你收集整理的使用Silverlight Toolkit 应用以及动态切换主题全部内容,希望文章能够帮你解决使用Silverlight Toolkit 应用以及动态切换主题所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。