1,在后台添加调用样式文件的方法
void UpdateTheme()
{
if (Global.OldTheme != Global.CurrentTheme)
{
Global.OldTheme = Global.CurrentTheme;
ResourceDictionary rd = new ResourceDictionary();
if (Global.CurrentTheme == "Red")
{
Application.LoadComponent(rd, new Uri("/ILS.Silverlight;component/Theme/Red.xaml", UriKind.Relative));
}
else if (Global.CurrentTheme == "Green")
{
Application.LoadComponent(rd, new Uri("/ILS.Silverlight;component/Theme/Green.xaml", UriKind.Relative));
}
else
{
Application.LoadComponent(rd, new Uri("/ILS.Silverlight;component/Theme/Blue.xaml", UriKind.Relative));
}
if(Application.Current.Resources.MergedDictionaries.Count>0)
{
Application.Current.Resources.MergedDictionaries.RemoveAt(0);
}
Application.Current.Resources.MergedDictionaries.Add(rd);
gPageBackground.Style = StyleHelper.FindResource("PageBackground") as Style;
borderTop.Style = StyleHelper.FindResource("MainPageBorder") as Style;
borderLeft.Style = StyleHelper.FindResource("MainPageBorder") as Style;
_MainPanel.Style = StyleHelper.FindResource("MainPageBorder") as Style;
lblVersion.Style = StyleHelper.FindResource("TextBlockStyle") as Style;
txtWelcome2.Style = StyleHelper.FindResource("TextBlockStyle") as Style;
lblMainPageLibraryName.Style = StyleHelper.FindResource("MainPageLibraryName") as Style;
txtWelcome.Style = StyleHelper.FindResource("MainPageLibraryName") as Style;
BorderMainPageLeft.Style = StyleHelper.FindResource("MainPageLefrBorderAndBG") as Style;
RTBILS.Style = StyleHelper.FindResource("RadToolBarStyle") as Style;
rtbInstall.Style = StyleHelper.FindResource("RadToolBarStyle") as Style;
_MainPanel.Style = StyleHelper.FindResource("MainPageWorkPanelBorder") as Style;
System1.Style = System2.Style = System3.Style = StyleHelper.FindResource("MainSystemForeground") as Style;
this.UpdateLayout();
}
}
但是这样只能对没有Key值的样式和当前页面带Key值的样式起作用,对其他页面带Key值的样式不起作用。
因此有两种解决方法:1 当切换样式时,通过事件获取当前页面的实例,在后台重新调用所有带Key值控件的样式。 这种方法的缺点是,当页面控件很多时,切换样式的速度会很慢。
2 在样式文件中进行修改,样式文件可以通过连接的方式获取其他样式文件的样式。例如
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:Common="clr-namespace:ILS.Silverlight.Common;assembly=ILS.Silverlight.Common"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Name="Menu" Source="Base/BaseMenu.xaml"/>
<ResourceDictionary x:Name="Button" Source="Base/BaseButton.xaml"/>
<ResourceDictionary x:Name="Calendar" Source="Base/BaseCalendar.xaml"/>
<ResourceDictionary x:Name="Combobox" Source="Base/BaseCombobox.xaml"/>
<ResourceDictionary x:Name="Grid" Source="Base/BaseGrid.xaml"/>
<ResourceDictionary x:Name="Tab" Source="Base/BaseTab.xaml"/>
<ResourceDictionary x:Name="Text" Source="Base/BaseText.xaml"/>
<ResourceDictionary x:Name="ToolBar" Source="Base/BaseToolBar.xaml"/>
<ResourceDictionary x:Name="Treeview" Source="Base/BaseTreeview.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="Grid" x:Key="PageBackground">
<Setter Property="Background" Value="#FF39588E" />
</Style>
<!--Default Grid add by DuanHanLin 2011-12-13-->
<Style TargetType="Grid">
<Setter Property="Background" Value="#FF39588E"></Setter>
</Style>
</ResourceDictionary>
这样通过后台调用一个样式文件,就能调用其他样式文件。通过在链接样式文件里调用所需的样式,而在调用样式的样式文件里设置颜色,来获得切换样式的效果。