Silverlight 皮膚的使用(二)
開發環境:vs2010+sl4+ Toolkit April 2010
1、 安裝好Microsoft Silverlight 4 Toolkit April 2010
2、 打開Toolkit 安裝目錄下的 Themes/xaml 文件夾 , 你可以看到非常多的 xaml 主題文件
如圖:
3、 新建一個silverlight 項目,並在項目下建一個 Themes 文件夾
4、 把Toolkit 安裝目錄下的 Themes/xaml 文件夾下的文件拷貝到 Themes 下
效果如圖:
5、引入 dll ,命名空間(有好多,可以編譯根據錯誤提示查看需要引入什麼)
效果如圖:
6、 設計Demo 界面
右上角放一個combobox 用來選擇皮膚
中間愛放啥就放啥
7、 關鍵代碼
// 匹 配 路 徑
String skinStr = "Themes/System.Windows.Controls.Theming." + skinKey + ".xaml" ;
//獲取資源
Uri uri = new Uri (skinStr, UriKind .Relative);
//應用豬蹄
Theme .SetApplicationThemeUri( App .Current, uri);
8、 源碼
Xaml:
< UserControl x : Class ="SkinsDemoTwo.MainPage"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns : x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns : d ="http://schemas.microsoft.com/expression/blend/2008"
xmlns : mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc : Ignorable ="d"
d : DesignHeight ="300" d : DesignWidth ="400" xmlns : sdk ="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
< Grid x : Name ="LayoutRoot" Background ="White">
< Grid.RowDefinitions >
< RowDefinition Height ="30" />
< RowDefinition Height ="*" />
</ Grid.RowDefinitions >
< ComboBox x : Name ="cbSkins" Grid.Row ="0" Height ="30" Width ="100" HorizontalAlignment ="Right" SelectionChanged ="ComboBox_SelectionChanged">
</ ComboBox >
< sdk : Calendar Grid.Row ="1" Height ="168" HorizontalAlignment ="Left" Margin ="24,38,0,0" Name ="calendar1" VerticalAlignment ="Top" Width ="171" />
< Button Content ="Button" Grid.Row ="1" Height ="115" HorizontalAlignment ="Left" Margin ="236,64,0,0" Name ="button1" VerticalAlignment ="Top" Width ="144" />
</ Grid >
</ UserControl >
Cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Theming;
namespace SkinsDemoTwo
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
initSkins();
}
void initSkins() {
// <ComboBoxItem Content="BubbleCreme"></ComboBoxItem>
//<ComboBoxItem Content="BureauBlack"></ComboBoxItem>
//<ComboBoxItem Content="BureauBlue"></ComboBoxItem>
//<ComboBoxItem Content="ExpressionDark"></ComboBoxItem>
//<ComboBoxItem Content="ExpressionLight"></ComboBoxItem>
//<ComboBoxItem Content="RainierOrange"></ComboBoxItem>
//<ComboBoxItem Content="RainierPurple"></ComboBoxItem>
//<ComboBoxItem Content="ShinyBlue"></ComboBoxItem>
//<ComboBoxItem Content="ShinyRed"></ComboBoxItem>
//<ComboBoxItem Content="SystemColors"></ComboBoxItem>
//<ComboBoxItem Content="TwilightBlue"></ComboBoxItem>
//<ComboBoxItem Content="WhistlerBlue"></ComboBoxItem>
cbSkins.Items.Add( "BubbleCreme" );
cbSkins.Items.Add( "BureauBlack" );
cbSkins.Items.Add( "BureauBlue" );
cbSkins.Items.Add( "ExpressionDark" );
cbSkins.Items.Add( "ExpressionLight" );
cbSkins.Items.Add( "RainierOrange" );
cbSkins.Items.Add( "RainierPurple" );
cbSkins.Items.Add( "ShinyBlue" );
cbSkins.Items.Add( "ShinyRed" );
cbSkins.Items.Add( "SystemColors" );
cbSkins.Items.Add( "TwilightBlue" );
cbSkins.Items.Add( "WhistlerBlue" );
}
private void ComboBox_SelectionChanged( object sender, SelectionChangedEventArgs e)
{
//獲?取?你?選?擇?的?主÷題琣
String skinKey = cbSkins.SelectedValue.ToString();
//匹¥配?路·徑?
String skinStr = "Themes/System.Windows.Controls.Theming." + skinKey + ".xaml" ;
//獲?取?主÷題琣資哩?源′
Uri uri = new Uri (skinStr, UriKind .Relative);
//應畖用?主÷題琣
Theme .SetApplicationThemeUri( App .Current, uri);
}
}
}
6、 效果圖
歡迎轉載,請註明出處
QQ454162034 有問題可以共同討論