Silverlight4中實現Theme的動態切換

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   io   strong   ar   

  Silverlight一般用來開發一些企業的應用系統,如果使用者一直面對同一種風格的頁面,時間長了難免厭煩,所以一般都會提供好幾種風格及Theme供使用者選中,下面就來說一下如何在不重新登入系統的情況下,實現風格的動態切換。我們寫一個Demo來說明一下。

  建立一個Silverlight的項目,並添加一個預設的網站,先來寫一下頁面吧,簡單起見,只放兩個控制項,MainPage的代碼如下:

<UserControl x:Class="SilverlightChangeTheme.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">    <Grid x:Name="LayoutRoot" Background="White">      <Button Content="ChangeTheme" Height="23" HorizontalAlignment="Left"               Margin="40,66,0,0" Name="button1" VerticalAlignment="Top"               Width="108" Click="button1_Click" Style="{StaticResource BtnStyle}" />      <TextBlock Height="23" HorizontalAlignment="Left"                  Margin="58,37,0,0" Name="textBlock1"                  Text="文字樣式" VerticalAlignment="Top"                  FontSize="14" Width="101" Style="{StaticResource txtStyle}" />   </Grid></UserControl>

如下:

就是一個簡單的按鈕和一個TextBlock,下面要實現的效果是點擊按鈕實現文字和按鈕字型顏色改變(樣式要從資源檔載入);

那麼既然要實現切換風格,首先要有資源檔,我們先建兩個Silverlight Resource Dictionary,紅色風格和藍色風格,命名為Blue.xaml和Red.xaml,代碼如下:

Red.xaml:

<ResourceDictionary    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">   <Style TargetType="Button" x:Key="BtnStyle">      <Setter Property="Foreground" Value="Red" />   </Style>   <Style TargetType="TextBlock" x:Key="txtStyle">      <Setter Property="Foreground" Value="Red" />   </Style></ResourceDictionary>

Blue.xaml:

<ResourceDictionary    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">   <Style TargetType="Button" x:Key="BtnStyle">      <Setter Property="Foreground" Value="Blue" />   </Style>   <Style TargetType="TextBlock" x:Key="txtStyle">      <Setter Property="Foreground" Value="Blue" />   </Style></ResourceDictionary>

由於MainPage中已經綁定了資源檔中的樣式,所以系統啟動時就必須載入某一個樣式檔案,就是所謂的預設樣式,所以要加一個靜態類,來實現系統啟動時載入預設樣式和轉場樣式,代碼如下:

ThemeHelper.cs

using System;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Ink;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;namespace SilverlightChangeTheme{   public static class ThemeHelper   {      /// <summary>      /// 載入樣式檔案到Application.Current.Resources      /// </summary>      /// <param name="theme"></param>      public static void LoadTheme(string theme)      {         ResourceDictionary rd = new ResourceDictionary();         if (theme.Length == 3)         {            Application.LoadComponent(rd, new Uri("/SilverlightChangeTheme;component/Red.xaml", UriKind.Relative));         }         else         {            Application.LoadComponent(rd, new Uri("/SilverlightChangeTheme;component/Blue.xaml", UriKind.Relative));         }         //清空Application.Current.Resources,存放新的樣式         if (Application.Current.Resources.Count > 0)         {            Application.Current.Resources.Clear();         }         Application.Current.Resources.MergedDictionaries.Add(rd);      }      /// <summary>      /// 從Application.Current.Resources中擷取指定名稱的樣式      /// </summary>      /// <param name="name"></param>      /// <returns></returns>      public static object FindResource(string name)      {         if (App.Current.Resources.Contains(name))         {            return App.Current.Resources[name];         }         else         {            return null; //這裡返回NULL,如果沒有控制項會變成系統預設樣式。         }      }   }}

然後在App.xaml.cs中,在Application_Startup方法中添加如下代碼:

private void Application_Startup(object sender, StartupEventArgs e)      {         //載入預設樣式         ThemeHelper.LoadTheme("Blue");         this.RootVisual = new MainPage();      }

這樣啟動時就會自動載入藍色的風格了,下:

然後,給MainPage的Button添加事件,實現樣式的切換,代碼如下:

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;namespace SilverlightChangeTheme{   public partial class MainPage : UserControl   {      bool IsRed = false;      public MainPage()      {         InitializeComponent();      }      private void button1_Click(object sender, RoutedEventArgs e)      {         if (IsRed)         {            //載入樣式到Application.Current.Resources            ThemeHelper.LoadTheme("Blue");            IsRed = false;         }         else         {            //載入樣式到Application.Current.Resources            ThemeHelper.LoadTheme("Red");            IsRed = true;         }         //運行時,更新樣式必須重新設定每個控制項的Style,然後調用this.UpdateLayout()方法更新頁面。         button1.Style = ThemeHelper.FindResource("BtnStyle") as Style;         textBlock1.Style = ThemeHelper.FindResource("txtStyle") as Style;         this.UpdateLayout();      }   }}

這樣就實現了樣式的切換,需要注意的是系統啟動後轉場樣式,要在代碼中重新設定每個控制項的Style,然後調用this.UpdateLayout()方法更新頁面。

當然,如果有多表單的話,沒有開啟的話是不需要設定的,只需要設定已經顯示的控制項。

 

補充知識:

關於Uri路徑的寫法。

XAML檔案的相對Uri訪問, 如<Image Source="silverlight.png" />或是<Image Source="./silverlight.png" />,

在子檔案夾裡的可以用<Image Source=”./images/sl.jpg” />訪問到
最保險的方式是採用特有的程式集資源URI訪問,格式為
<Image Source="/{assemblyShortName};component/Foo.jpg"/>, 這種方式還可以引用到xap中的其他程式集中的圖片

 

修改我們的兩個圖片的引用方式為

<Image Source="/SilverlightApplication1;component/silverlight.png"/>
<Image Source="/SilverlightApplication1;component/images/sl.jpg" Height="100"/>  

 

點擊這裡下載源碼

 

Silverlight4中實現Theme的動態切換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.