1. 程式人生 > >WPF學習(10)-資源

WPF學習(10)-資源

       資源就是可以讓你在程式裡面呼叫的東西,有很多東西,比如需要多次呼叫,比如一個按鈕的樣式,每個頁面上的按鈕希望達成一個統一的風格,那麼這個時候就可以封裝成資源,然後直接呼叫這樣我們的xaml頁面就會顯得非常簡潔,比如下面這個頁面有十個按鈕。

    <Grid>
        <Grid.Resources>
            <Style TargetType="Button">
                <Setter Property="Background" Value="Red"></Setter>
                <Setter Property="Foreground" Value="Yellow"></Setter>
                <Style.Triggers >
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="Background" Value="Black"></Setter>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Grid.Resources>
        <Button  Content="Button" HorizontalAlignment="Left" Margin="63,147,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="178,147,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="178,212,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="63,212,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="318,147,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="318,212,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>

          這樣就給多個button使用了同一個樣式,效果如下

   

<Window.Resources>        
<!--<TextBlock x:Key="res1" Text="奔騮科技"></TextBlock>        
<TextBlock x:Key="res2" Text="智慧無線全解決方案供應商"></TextBlock>-->    </Window.Resources>

定義兩個資源,資源放在window窗體範圍內,那麼在這個範圍內,就可以任意使用這個資源。

<Button Content="{StaticResource res1}" HorizontalAlignment="Left" Margin="170,29,0,0" VerticalAlignment="Top" Width="200" Height="42"/>
<Button Content="{DynamicResource res2}" HorizontalAlignment="Left" Margin="170,110,0,0" VerticalAlignment="Top" Width="200" Height="42"/>

     直接繫結資源,其中一個使用staticresource,也就是靜態資源,另外一個適應dynamicresource,也就是動態資源,效果如下。

       靜態資源和動態資源的區別就是,靜態資源只讀取一次,以後就算更改,也不會改變,而動態資源會隨著資源的改變而改變,比如,我們在後臺來修改上面兩個資源。通過前臺頁面上的一個按鈕,註冊一個事件,通過This.Resources[]鍵去修改他的值,結果就是其中一個會改變,另外一個不會,而改變的就是動態資源,不變的就是靜態資源

 

 this.Resources["res1"] = "室內定位系統";
 this.Resources["res2"] = "無線感測系統";

 

        當然更多的時候,我們不會在頁面中定義資源,而是在外部定義資源,然後到頁面中引用,這樣的好處是如果需要修改,直接到對應的那塊去修改即可。我們先來定義一個外部的資源,直接右擊我們的解決方案的專案,右擊新增,選擇資源字典。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <sys:String x:Key="res1" >123</sys:String>
    <sys:String x:Key="res2" >123</sys:String>
</ResourceDictionary>

      當然,不能忘記的是,要新增名稱空間,不然xaml直譯器不知道啥是字串,到任意一個頁面繫結資源。

       

    <Window.Resources>
        <ResourceDictionary Source="Dictionary1.xaml">           
        </ResourceDictionary>
    </Window.Resources>

    新增頁面對於資源字典的引用,這樣就可以在頁面中引用了。

 <Button Content="{StaticResource res1}" HorizontalAlignment="Left" Margin="170,29,0,0" VerticalAlignment="Top" Width="200" Height="42"/>
 <Button Content="{DynamicResource res2}" HorizontalAlignment="Left" Margin="170,110,0,0" VerticalAlignment="Top" Width="200" Height="42"/>

     效果如下:

      當然,我們可以對於資源字典擴充,比如後期,我們有一個需求,需要修改按鈕的背景色為漸變色畫刷,那麼也很好改,直接在Dictionary1.xaml中修改即可。

      

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <sys:String x:Key="res1" >我是res1對應的資源</sys:String>
    <sys:String x:Key="res2" >我是res2對應的資源</sys:String>
    <LinearGradientBrush x:Key="mybrush">
        <GradientStop Color="Red" Offset="0"/>
        <GradientStop Color="Gray" Offset="1"/>
    </LinearGradientBrush>
</ResourceDictionary>

     

<Button Content="{StaticResource res1}" HorizontalAlignment="Left" Margin="170,29,0,0" VerticalAlignment="Top" Width="200" Height="42" Background="{StaticResource mybrush}"/>

     效果就出來了,非常方便,而且頁面的xaml程式碼也少了很多 ,最重要的是別的頁面也可以用,風格就統一啦。