关于Silverlight 皮肤切换的的解决方法.

1 篇文章 0 订阅

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>

这样通过后台调用一个样式文件,就能调用其他样式文件。通过在链接样式文件里调用所需的样式,而在调用样式的样式文件里设置颜色,来获得切换样式的效果。

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值