天天看點

WPF快速指導1:資源

WPF快速指導1:資源

    本文摘要:

    1:資源應用場景;

    2:靜态資源和動态資源;

    3:Application.Current.Resources和Application.Current.Resources.MergedDictionaries

    4:路徑

一:資源的應用場景

場景1:格式化界面顯示元素

    所謂格式化界面顯示元素,就是使用統一的風格來定義軟體的每個界面。

    要滿足本需求,隻需要在App.xaml中如下定義資源

<Application.Resources>
        <Style TargetType="TextBlock" x:Key="TitleText">
            <Setter Property="Background" Value="Blue"/>
            <Setter Property="FontSize" Value="12"/>
        </Style>
        <Style TargetType="TextBlock" x:Key="Label">
            <Setter Property="Background" Value="Blue"/>
            <Setter Property="FontSize" Value="12"/>
        </Style>
</Application.Resources>      

同時,在每個頁面如下引用資源即可:

<StackPanel>
        <TextBlock Style="{StaticResource TitleText}">Title</TextBlock>
        <TextBlock Style="{StaticResource Label}">Label</TextBlock>
    </StackPanel>      

場景2:動态更新界面風格

    要動态更新界面風格,首先需要定義多種界面風格。假設有Sytle1和Style2兩種風格,其中Style1在Style1.xaml中定義:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="TextBlock" x:Key="TitleText">
        <Setter Property="Background" Value="Blue"/>
        <Setter Property="FontSize" Value="12"/>
    </Style>
    <Style TargetType="TextBlock" x:Key="Label">
        <Setter Property="Background" Value="Blue"/>
        <Setter Property="FontSize" Value="12"/>
    </Style>
</ResourceDictionary>      

Style2在Style2.xaml中定義(假設将Style1中的Blue改為Yellow,12改為24,不再列出)。那麼,在頁面中,我們如下引用:

<StackPanel>
        <TextBlock Style="{DynamicResource TitleText}">Title</TextBlock>
        <TextBlock Style="{DynamicResource Label}">Label</TextBlock>
        <Button Click="Button1_Click">Style1</Button>
        <Button Click="Button2_Click">Style2</Button>
    </StackPanel>      

同時,背景代碼為:

ResourceDictionary style1;
        ResourceDictionary style2;
        
        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            style1 = new ResourceDictionary();
            style1.Source = new Uri(@"Resouce\Style1.xaml", UriKind.Relative);
            Application.Current.Resources = style1;
        }
        private void Button2_Click(object sender, RoutedEventArgs e)
        {
            style2 = new ResourceDictionary();
            style2.Source = new Uri(@"Resouce\Style2.xaml", UriKind.Relative);
            Application.Current.Resources = style2;
        }      

    如此一來,我們便完成動态的界面風格變化。

二:靜态資源和動态資源

    在上文的場景二示例中,如果将XAML中的

<TextBlock Style="{DynamicResource TitleText}">Title</TextBlock>
        <TextBlock Style="{DynamicResource Label}">Label</TextBlock>      

    換成

<TextBlock Style="{StaticResource TitleText}">Title</TextBlock>
        <TextBlock Style="{StaticResource Label}">Label</TextBlock>      

    我們會發現界面的風格根本沒有得到改變。這裡我們引出靜态資源和動态資源最重要的一個差別:靜态資源不基于運作時行為進行重新求值,而動态資源在運作時加載。

    關于靜态資源和動态資源其它差別請檢視MSDN。

三:Application.Current.Resources和Application.Current.Resources.MergedDictionaries

    先來看這兩個變量的原型:

    Application.Current.Resources的原型是一個ResourceDictionary。

    Application.Current.Resources.MergedDictionaries是一個Collection<ResourceDictionary> 。

    從本質上來講,這兩個變量沒有差別,MergedDictionaries是在表現形式上,在運作時擴充系統的資源。

    我們再來看上文中運作時動态改變界面的示例,我們通過動态給Application.Current.Resources指派,來改變界面風格。

    在實際使用中,必不要這麼做。因為不管你是否需要在運作時動态更新部分界面風格,有些資源是肯定不變的。也就是說,一個系統,必定已經存在一個資源檔案,即,最好不要在運作時改變Application.Current.Resources。那麼,實際要做的,就是動态的增加或者删除Application.Current.Resources.MergedDictionaries就可以了。

四:路徑

  第一種:

imgContent.Source = new BitmapImage(new Uri("Content.jpg", UriKind.Relative)); 
            imgResource.Source = new BitmapImage(new Uri("Resource.jpg", UriKind.Relative));      

  第二種:

imgContent.Source = new BitmapImage(new Uri("pack://application:,,,/Content.jpg")); 
            imgResource.Source = new BitmapImage(new Uri("pack://application:,,,/Resource.jpg"));      

  第三種:

imgContent.Source = new BitmapImage(new Uri("pack://SiteOfOrigin:,,,/Content.jpg"));       

  最後一點需要說說的是路徑的問題,關于路徑,在WPF中有幾種表示方法:

  第一種和第二種都可以通路相對WPF資源路徑的Resource和Content資源。第三種方式可以通路運作目錄下的Content資源檔案以及完全松散的檔案。完全松散的檔案指那些沒有添加到項目中,隻是拷貝在程式目錄中的檔案。

  應用程式根本不知道它的存在。pack://application:,,,/Content.jpg表示目前項目的資源。它是pack://application:,,,/DllName;Component/Content.jpg的簡寫。将DllName替換成其他程式集,就可以通路其他程式集的資源。

  pack://SiteOfOrigin:,,,/Content.jpg表示從部署位置通路檔案。

  pack URI格式是XML檔案規範的一部分,具體格式如下 pack://packageURI/partPath。PackageURI實際上是在URI中放一個URI,它是把反斜杠都變成了逗号。packageURI的WPF資源路徑可以志向一個XPS文檔,例如file : /// c: /Document . xps會被編碼為file:...c:,Document.xps。在WPF程式中有兩種URI系統是特别處理的:

  siteOfOrigin:/// 編碼後siteOfOrigin:,,,

  application:/// 編碼後application:,,,

  3個逗号其實是反斜杠編碼過來的。

該系列參考:MSDN、《WPF程式設計》

練習:

1:要統一整個應用程式的風格,應該如何來處理。

2:要對某個特定的頁面(Window或Page)上所有的TextBlock統一風格,應該如何處理。

3:靜态資源和動态資源的一個差別。

4:Application.Current.Resources和Application.Current.Resources.MergedDictionaries的用處。

WPF快速指導1:資源

本文基于

Creative Commons Attribution 2.5 China Mainland License

釋出,歡迎轉載,演繹或用于商業目的,但是必須保留本文的署名

http://www.cnblogs.com/luminji

(包含連結)。如您有任何疑問或者授權方面的協商,請給我留言。