代码改变世界

Silverlight 皮肤(主题)动态切换

2012-08-09 22:32  图安  阅读(745)  评论(0编辑  收藏  举报

Silverlight主题切换比较简单,只需要简单的几部就可以轻松的实现。

首先引用dll :System.Windows.Controls.Theming.Toolkit

其次在工程里将皮肤文件包括进来

 

 

然后在xaml文件里写入如下代码

<TextBlock Name="tbThems" Width="Auto" VerticalAlignment="Center" Text="主题切换:" FontSize="12" Margin="0,0,0,0"/>

<ComboBox x:Name="combThemes" Height="25" Width="150"
SelectionChanged="combThemes_SelectionChanged">
<ComboBoxItem Content="默认主题" IsSelected="True"></ComboBoxItem>
<ComboBoxItem Content="JetPack" Tag="JetPack.xaml" ></ComboBoxItem>
<ComboBoxItem Content="AccentColor" Tag="AccentColor.xaml" ></ComboBoxItem>
<ComboBoxItem Content="Cosmopolitan" Tag="Cosmopolitan.xaml" ></ComboBoxItem>
<ComboBoxItem Content="Windows7" Tag="Windows7.xaml" ></ComboBoxItem>
</ComboBox>

 

后台cs文件写入如下代码:


/// <summary>
/// 系统主题切换
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void combThemes_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBoxItem comb =(ComboBoxItem)((ComboBox)sender).SelectedItem;
if (comb.Tag==null)
{
Theme.SetApplicationThemeUri(App.Current, null);
}
else if (comb.Tag != null)
{
string themeName = comb.Tag.ToString();
if (!String.IsNullOrEmpty(themeName))
{
SetTheme(themeName);
}
}
}
private void SetTheme(string sTheme)
{
Uri themeUri = new Uri(string.Format(@"/Shhc.MonitorPlatform.Dump.SL;component/Assets/Themes/{0}", sTheme), UriKind.Relative);
Theme.SetApplicationThemeUri(App.Current, themeUri);
}

 

这样就可以实现一个简单的动态切换皮肤了。