使用Silverlight Toolkit中的主题(Theme)

来源:互联网 发布:电脑网页版淘宝网首页 编辑:程序博客网 时间:2024/06/05 16:54

 

      在Silverlight Tookit 中提供了大约十种主题,大家可以根据自己的喜好,很容易就在项目中实现
动态换肤效果。当然其官方还推荐了几个制作主题的插件,使用这些 Blend插件可以很方便的生成各
格颜色的主题。

      好了,下面开始今天的正文。
    
     首先我们要下载该Tookit并将其中的相应DLL文件:Microsoft.Windows.Controls.Theming.dll加
到当前的示例中,另外就是相关的theme文件了,我已将10种主题文件放在了这个DEMO的themes
文件夹下:

       

        并以“内容”方式作为"生成操作"的选项,如下:

     

        我们可以直接在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下的所有控件样式均应用了ExpressionLight主题。另外我们也可以在CS
文件中对指定的控件设置相应的主题,比如本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[
0as ComboBoxItem);
}

private void ThemeList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    SetTheme(ThemeList.SelectedItem 
as ComboBoxItem);
}

//设置相应的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。
可以从这个链接中获得一些信息:)  
   

     好了,今天的内容就先到这里了,源码下载,请点击这里。
    
    
     在微软的Silverlight 开源控件项目: Silverlight Toolkit

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 铁耳屎在最里面怎么办 婴儿鼻屎特别深怎么办 鼻子干呼吸就疼怎么办 儿童鼻子里总有好多鼻屎怎么办 小孩鼻子里有鼻屎呼吸不通怎么办 鼻子不通通气鼻屎粘在鼻子怎么办 鼻子里面干燥长鼻屎怎么办 鼻孔里干的难受怎么办 每天有很多鼻屎怎么办 鼻子里面干的疼怎么办 婴儿鼻屎堵住了怎么办 婴儿有很多鼻屎怎么办 隆鼻7天好多鼻屎怎么办 隆鼻第五天好多鼻屎怎么办 小孩鼻屎堵住了怎么办 风寒感冒流清鼻涕怎么办 流清鼻涕吐黄痰不发烧怎么办 宝宝流黄鼻涕发烧怎么办 感冒了浓鼻涕多怎么办 感冒流浓鼻涕怎么办速效办法 孩子一直流清水鼻涕怎么办 宝宝鼻子呼噜呼噜响怎么办 鼻涕往嗓子里流怎么办 咳嗽痰多鼻涕多怎么办 没感冒嗓子痰多鼻涕怎么办 孩子感冒后鼻涕特别多怎么办 经常有鼻涕怎么办才好 怀孕后鼻涕痰多怎么办 鼻炎有鼻涕痰多怎么办 宝宝咳嗽痰多鼻涕多怎么办 宝宝两岁清鼻涕咳嗽痰多怎么办 喉咙咸咸的有痰怎么办 宝宝咳嗽鼻塞喉咙有痰怎么办 绝地求生刺激战场射击键误触怎么办 在皮卡堂卡的游泳了怎么办 假如遇到老赖没能力还钱怎么办 服刑人拒不执行伤害赔偿怎么办? 面对当前严峻形势作为军人怎么办 想起诉不知道对方地址怎么办 遇见家里来嫌疑人员怎么办 老滚5老婆死了怎么办