在Silverlight Tookit 中提供了大約十種主題,大家可以根據(jù)自己的喜好,很容易就在項目中實現(xiàn)
動態(tài)換膚效果。當然其官方還推薦了幾個制作主題的插件,使用這些 Blend插件可以很方便的生成各種風(fēng)格顏色的主題。
好了,下面開始今天的正文。
首先我們要下載該Tookit并將其中的相應(yīng)DLL文件:Microsoft.Windows.Controls.Theming.dll加
載到當前的示例中,另外就是相關(guān)的theme文件了,我已將10種主題文件放在了這個DEMO的themes
文件夾下:
并以“內(nèi)容”方式作為"生成操作"的選項,如下:
我們可以直接在XAML文件中聲明使用主題的元素,比如:
<UserControl
..
xmlns:theming="clr-namespace:Microsoft.Windows.Controls.Theming;assembly=Microsoft.Windows.Controls.Theming"

>
<!--ShinyDarkPurple-->
<StackPanel Width="100"
theming:ImplicitStyleManager.ApplyMode="Auto"
theming:ImplicitStyleManager.ResourceDictionaryUri="themes/ExpressionLight.xaml">
<Button Content="Button"/>
<CheckBox Content="CheckBox"/>
<RadioButton Content="RadioButton"/>
<Slider/>
<ListBox/>
<ProgressBar Height="15" Value="30"/>
<controls:Expander ExpandDirection="Down"/>
</StackPanel>
<!--ShinyDarkGreen-->
</UserControl>
這樣在該StackPanel下的所有控件樣式均應(yīng)用了ExpressionLight主題。另外我們也可以在CS
文件中對指定的控件設(shè)置相應(yīng)的主題,比如本DEMO中所寫的代碼:
public Page()
{
InitializeComponent();
this.ThemeList.SelectionChanged += new SelectionChangedEventHandler(ThemeList_SelectionChanged);
this.Loaded += new RoutedEventHandler(Page_Loaded);
}
void Page_Loaded(object sender, RoutedEventArgs e)
{
ThemeList.Items.Add(new ComboBoxItem() { Name = "ExpressionDark", Content = "ExpressionDark", DataContext = "themes/ExpressionDark.xaml", IsEnabled = true });
ThemeList.Items.Add(new ComboBoxItem() { Name = "ExpressionLight", Content = "ExpressionLight", DataContext = "themes/ExpressionLight.xaml" });
ThemeList.Items.Add(new ComboBoxItem() { Name = "RainierOrange", Content = "RainierOrange", DataContext = "themes/RainierOrange.xaml" });
ThemeList.Items.Add(new ComboBoxItem() { Name = "RainierPurple", Content = "RainierPurple", DataContext = "themes/RainierPurple.xaml" });
ThemeList.Items.Add(new ComboBoxItem() { Name = "RainierRadialBlue", Content = "RainierRadialBlue", DataContext = "themes/RainierRadialBlue.xaml" });
ThemeList.Items.Add(new ComboBoxItem() { Name = "ShinyBlue", Content = "ShinyBlue", DataContext = "themes/ShinyBlue.xaml" });
ThemeList.Items.Add(new ComboBoxItem() { Name = "ShinyDarkGreen", Content = "ShinyDarkGreen", DataContext = "themes/ShinyDarkGreen.xaml" });
ThemeList.Items.Add(new ComboBoxItem() { Name = "ShinyDarkPurple", Content = "ShinyDarkPurple", DataContext = "themes/ShinyDarkPurple.xaml" });
ThemeList.Items.Add(new ComboBoxItem() { Name = "ShinyDarkTeal", Content = "ShinyDarkTeal", DataContext = "themes/ShinyDarkTeal.xaml" });
ThemeList.Items.Add(new ComboBoxItem() { Name = "ShinyRed", Content = "ShinyRed", DataContext = "themes/ShinyRed.xaml" });
SetTheme(ThemeList.Items[0] as ComboBoxItem);
}
private void ThemeList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SetTheme(ThemeList.SelectedItem as ComboBoxItem);
}
//設(shè)置相應(yīng)的theme
void SetTheme(ComboBoxItem comboBoxItem)
{
if (comboBoxItem != null)
{
ControlPage control = new ControlPage();
Test.Children.Clear();
Test.Children.Add(control);
Uri uri = new Uri(comboBoxItem.DataContext.ToString(), UriKind.Relative);
ImplicitStyleManager.SetResourceDictionaryUri(control, uri);
ImplicitStyleManager.SetApplyMode(control, ImplicitStylesApplyMode.Auto);
ImplicitStyleManager.Apply(control);
}
}
上面代碼中的ControlPage 類即是我們要加載的控件頁對象,在該對象上聲明了一些控件,然后將這些控
件(集合)做為子控件加載到當前PAGE頁面中的Stack元素(Test)中。這樣我們運行一下這個DEMO,看一下
各個主題的顯示效果:

當然,官方還推薦了幾個主題制作插件工具,比如: Kuler,以及插件Colorful Expression。
可以從中獲得一些信息:)
好了,今天的內(nèi)容就先到這里了,源碼下載,請。
標簽:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:博客園