天天看點

windows phone (12) 小試自定義樣式

  <!--應用程式資源-->

    <Application.Resources>

    </Application.Resources>

 我們隻需要在上面标簽中加入我們自定義的樣式即可,适用于此資源的對象是有FrameworkElement派生的類,此類派生類的清單如下:

 以上類或者以上類中派生的類都可以使用此共享資源,這裡是指自定義樣式,接下來按照上一篇内容的做法,我們給内容區域的Textblock設定前景色,是以在App.xaml 自定義樣式可以這樣:

 <!--應用程式資源-->

        <LinearGradientBrush x:Key="lgBrush">

            <GradientStop Offset="0" Color="AliceBlue"></GradientStop>

            <GradientStop Offset="1" Color="BurlyWood"></GradientStop>

        </LinearGradientBrush>

 x:Key特性是唯一标示該資源的一個鍵名,在共享資源中必須唯一;自定義樣式定義好了,怎麼使用那,比較繁瑣的做法是這樣,不提倡:

<TextBlock x:Name="tbContent" Text="顯示樣式" HorizontalAlignment="Center" VerticalAlignment="Center">

            <TextBlock.Foreground>

                <StaticResource ResourceKey="lgBrush"></StaticResource>

            </TextBlock.Foreground>

        </TextBlock>

 比較常用的書寫是這樣:

  <TextBlock x:Name="tbContent" Text="顯示樣式" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="{StaticResource lgBrush}">

            </TextBlock>

 可以看到這裡有個大括号,它就是xaml标記擴充,在xaml标記擴充中是不能使用引号的,比如這裡的lgBrush不能使用引号;上面兩種方法實作的效果一樣:即

此 外我們還可以看到MainPage類在xaml檔案中已經定義了Foreground,但是在tbContent中我們依然看到了我們自定義的顔色,這說 明樣式設定的優先級高于繼承來的樣式的優先級;以上兩種方法是實作在xaml檔案中對樣式的使用,我們也可以在隐藏檔案(.cs)進行通路,但是必須是在 構造函數完成之後,例如我們可以這樣通路剛剛定義的樣式:

View Code

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 Microsoft.Phone.Controls;

namespace ShareStyle

{

    public partial class MainPage : PhoneApplicationPage

    {

        // 構造函數

        public MainPage()

        {

            InitializeComponent();

            LinearGradientBrush lgBrush = (LinearGradientBrush)this.Resources["lgBrush"];

            TextBlock tb = new TextBlock();

            tb.Name = "tbName";

            tb.VerticalAlignment = VerticalAlignment.Center;

            tb.HorizontalAlignment = HorizontalAlignment.Center;

            tb.Text = "隐藏代碼執行個體化的";

            tb.Foreground = lgBrush;

            this.ContentPanel.Children.Add(tb);

        }

    }

}

如果想使用該樣式的話,就像上面的代碼執行個體化樣式,并設定Textblock的前景色為lgBrush,還有另一種寫法是将自定義樣式中的x:Key改為x:Name,隐藏檔案中設定前景色就可以是這樣:(此處有疑問:根據教材中的說法,我怎麼也擷取不到設定的顔色)

 tb.Foreground = lgBrush;

 不需要執行個體化該自定義顔色,需要注意的是如果使用x:Name資源,該名稱必須是在xaml檔案中保持唯一性;

 上面的案例是說明怎麼自定義某個屬性(Foreground )的樣式,下面是為特定的元素定義樣式集合

<phone:PhoneApplicationPage>

<phone:PhoneApplicationPage.Resources>

        <Style x:Key="tbStyle" TargetType="TextBlock">

         <Setter Property="HorizontalAlignment" Value="Center"></Setter>

        </Style>

    </phone:PhoneApplicationPage.Resources>

</phone:PhoneApplicationPage>

 上面執行個體代碼中x:Key表示鍵名,在使用該樣式的時候會用到,TargetType是指此樣式的使用對象元素,Style标簽中Setter标簽是設定适用此樣式的元素屬性;

執行個體代碼:

    <phone:PhoneApplicationPage.Resources>

            <Setter Property="HorizontalAlignment" Value="Center"></Setter>

            <Setter Property="Foreground">

                <Setter.Value>

                    <LinearGradientBrush>

                        <GradientStop Offset="0.2" Color="Brown"></GradientStop>

                        <GradientStop Offset="0.7" Color="DarkBlue"></GradientStop>

                    </LinearGradientBrush>

                </Setter.Value>

            </Setter>

        </Style>

 在TextBlock元素中的使用xaml标記擴充得到該樣式:

<TextBlock x:Name="tbContent" Text="顯示樣式" HorizontalAlignment="Center" VerticalAlignment="Center" Style="{StaticResource tbStyle}"  />

 得到的效果是這樣子的:

本文轉自shenzhoulong  51CTO部落格,原文連結:http://blog.51cto.com/shenzhoulong/836418,如需轉載請自行聯系原作者

繼續閱讀