WPF不僅支持傳統的程序級的資源,還有獨具特色的對象級資源,每一個界面元素,都可以擁有自己的資源,並被子元素共享。本文以一些簡單的小例子,簡述WPF中資源的相關用法,僅供學習分享使用,如有不足之處,還請指正。
基礎用法
通常情況下,資源是在Window.Resources節點下,便於Window下所有的子元素共享,如下示例所示:
示例源碼
定義一個字符串類型的資源,在TextBlock中通過Text="{StaticResource default}"的方式進行引用。如下所示:
1 <Window x:Class="WpfApp1.TenWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApp1" 7 xmlns:sys="clr-namespace:System;assembly=mscorlib" 8 mc:Ignorable="d" 9 Title="資源基礎示例" Height="250" Width="400"> 10 <Window.Resources> 11 <sys:String x:Key="default"> 12 沉舟側畔千帆過,病樹前頭萬木春 13 </sys:String> 14 </Window.Resources> 15 <Grid> 16 <TextBlock x:Name="tbInfo" Text="{StaticResource default}" FontSize="20" Margin="10" Padding="10" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock> 17 </Grid> 18 </Window>
資源層級
WPF資源是采用從內到外,逐層進行查找的,如果在當前窗口未檢索到資源,則繼續到App.xaml中繼續查找,示例如下所示:
在App.xaml中定義資源,然后在Window中應用資源,如下所示:
1 <Application x:Class="WpfApp1.App" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:local="clr-namespace:WpfApp1" 5 xmlns:sys="clr-namespace:System;assembly=mscorlib" 6 StartupUri="TenWindow.xaml"> 7 <Application.Resources> 8 <sys:String x:Key="story"> 9 懷舊空吟聞笛賦,到鄉翻似爛柯人。 10 </sys:String> 11 </Application.Resources> 12 </Application>
在Window窗口中調用,和調用本地資源是一樣的,如下所示:
1 <Window x:Class="WpfApp1.TenWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApp1" 7 xmlns:sys="clr-namespace:System;assembly=mscorlib" 8 mc:Ignorable="d" 9 Title="資源基礎示例" Height="250" Width="400"> 10 <Window.Resources> 11 <sys:String x:Key="default"> 12 沉舟側畔千帆過,病樹前頭萬木春。 13 </sys:String> 14 </Window.Resources> 15 <Grid> 16 <Grid.RowDefinitions> 17 <RowDefinition></RowDefinition> 18 <RowDefinition></RowDefinition> 19 </Grid.RowDefinitions> 20 <TextBlock x:Name="tbInfo1" Grid.Row="0" Text="{StaticResource story}" FontSize="20" Margin="10" Padding="10" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock> 21 <TextBlock x:Name="tbInfo2" Grid.Row="1" Text="{StaticResource default}" FontSize="20" Margin="10" Padding="10" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock> 22 </Grid> 23 </Window>
資源分類
根據資源的加載時間點,資源分為兩類,如下所示:
- 靜態資源:靜態資源是在程序啟動初始化時進行加載且只加載一次的資源。
- 動態資源:動態資源是在程序執行過程中,動態的去訪問資源,會隨着資源的改變而改變,所以動態資源對系統的消耗相對比較大。
動態資源
上述的基礎示例,采用的是靜態資源的方式。動態資源則是在程序執行過程中隨着資源的改變而改變。
兩個按鈕使用同一個資源【背景圖片】,只是一個采用靜態資源引用,一個采用動態資源引用,當資源發生改變時,一個不改變,一個實時變化。如下所示:
示例源碼,如下所示:
1 <Window x:Class="WpfApp1.NineWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApp1" 7 mc:Ignorable="d" 8 Title="資源基礎示例" Height="320" Width="400"> 9 <Window.Resources> 10 <!--ViewportUnits——設置平鋪的相對/絕對坐標,即圖片在哪平鋪。--> 11 <ImageBrush x:Key="one" Viewport="0 0 50 50" ViewportUnits="Absolute" TileMode="Tile" ImageSource="alan_logo.png" Opacity="0.3"></ImageBrush> 12 </Window.Resources> 13 <StackPanel Margin="5" x:Name="stackpanel1"> 14 <Button Content="第一個按鈕" Name="first" Margin="5" Padding="25" FontSize="58" Background="{ StaticResource one}"></Button> 15 <Button Content="第二個按鈕" Name="second" Margin="5" Padding="25" FontSize="58" Background="{ DynamicResource one}" Click="second_Click" ></Button> 16 </StackPanel> 17 </Window>
后台修改資源的代碼如下所示:
1 private void second_Click(object sender, RoutedEventArgs e) 2 { 3 var img = this.FindResource("one") as ImageBrush ; 4 img = new ImageBrush(new BitmapImage(new Uri(@"imgs/alan_logo1.png", UriKind.Relative))); 5 img.TileMode = TileMode.Tile; 6 img.Opacity = 0.3; 7 img.Viewport = new Rect(0, 0, 50, 50); 8 img.ViewportUnits = BrushMappingMode.Absolute; 9 this.Resources["one"] = img; 10 //注意:此處是直接重寫覆蓋資源key=one的對象,並不是對原資源設置ImageSoure屬性。兩者效果不同 11 }
資源文件
資源文件位於Properties/Resources.resx中,如果想要在程序中訪問資源文件的內容,則必須將訪問修飾符設置成public,如下所示:
在WPF中,通過Text="{x:Static prop:Resources.Password}"的方式,進行訪問資源內容,示例如下:
示例源碼如下:
1 <Window x:Class="WpfApp1.ElevenWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApp1" 7 xmlns:prop="clr-namespace:WpfApp1.Properties" 8 mc:Ignorable="d" 9 Title="資源文件示例" Height="150" Width="400"> 10 <Grid> 11 <Grid.RowDefinitions> 12 <RowDefinition></RowDefinition> 13 <RowDefinition></RowDefinition> 14 <RowDefinition></RowDefinition> 15 </Grid.RowDefinitions> 16 <Grid.ColumnDefinitions> 17 <ColumnDefinition Width="1*"></ColumnDefinition> 18 <ColumnDefinition Width="2*"></ColumnDefinition> 19 </Grid.ColumnDefinitions> 20 <TextBlock x:Name="tbUserName" Text="{x:Static prop:Resources.UserName}" VerticalAlignment="Center" HorizontalAlignment="Right" Grid.Row="0" Grid.Column="0" Margin="5"></TextBlock> 21 <TextBox x:Name="txtUserName" Grid.Row="0" Grid.Column="1" Margin="5"></TextBox> 22 <TextBlock x:Name="tbPassword" Text="{x:Static prop:Resources.Password}" VerticalAlignment="Center" HorizontalAlignment="Right" Grid.Row="1" Grid.Column="0" Margin="5"></TextBlock> 23 <TextBox x:Name="txtPassword" Grid.Row="1" Grid.Column="1" Margin="5"></TextBox> 24 <Button x:Name="btnSubmit" Content="{x:Static prop:Resources.Submit}" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Width="150" Margin="5"></Button> 25 </Grid> 26 </Window>
資源字典
資源字典可以實現資源的共享,一份定義,多處使用的效果。具有可維護性,高效,適應性等優勢。
首先創建資源字典文件,通過程序右鍵--添加--資源字典,打開資源字典對話框,創建名稱為OneDictionary.xaml,如下所示:
資源字典中創建了五個資源,如下所示:
1 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 2 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 3 xmlns:sys="clr-namespace:System;assembly=mscorlib" 4 xmlns:local="clr-namespace:WpfApp1"> 5 <sys:String x:Key="story0">酬樂天揚州初逢席上見贈</sys:String> 6 <sys:String x:Key="story1">【作者】劉禹錫 【朝代】唐</sys:String> 7 <sys:String x:Key="story2">巴山楚水凄涼地,二十三年棄置身。</sys:String> 8 <sys:String x:Key="story3">懷舊空吟聞笛賦,到鄉翻似爛柯人。</sys:String> 9 <sys:String x:Key="story4">沉舟側畔千帆過,病樹前頭萬木春。</sys:String> 10 <sys:String x:Key="story5">今日聽君歌一曲,暫憑杯酒長精神。</sys:String> 11 </ResourceDictionary>
在對應窗口中,包含資源文件的路徑即可,如下所示:
1 <Window x:Class="WpfApp1.TwelveWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApp1" 7 mc:Ignorable="d" 8 Title="資源字典示例" Height="350" Width="400"> 9 <Window.Resources> 10 <ResourceDictionary> 11 <ResourceDictionary.MergedDictionaries> 12 <ResourceDictionary Source="OneDictionary.xaml"></ResourceDictionary> 13 </ResourceDictionary.MergedDictionaries> 14 </ResourceDictionary> 15 </Window.Resources> 16 <StackPanel Margin="5" HorizontalAlignment="Center"> 17 <TextBlock x:Name="tbStory0" Margin="5" Padding="5" FontSize="20" Text="{StaticResource story0}"></TextBlock> 18 <TextBlock x:Name="tbStory1" Margin="5" Padding="5" FontSize="20" Text="{StaticResource story1}"></TextBlock> 19 <TextBlock x:Name="tbStory2" Margin="5" Padding="5" FontSize="20" Text="{StaticResource story2}"></TextBlock> 20 <TextBlock x:Name="tbStory3" Margin="5" Padding="5" FontSize="20" Text="{StaticResource story3}"></TextBlock> 21 <TextBlock x:Name="tbStory4" Margin="5" Padding="5" FontSize="20" Text="{StaticResource story4}"></TextBlock> 22 <TextBlock x:Name="tbStory5" Margin="5" Padding="5" FontSize="20" Text="{StaticResource story5}"></TextBlock> 23 </StackPanel> 24 </Window>
示例截圖如下:
以上就是資源的簡要介紹,主要用於拋磚引玉,共同學習,一起進步。
備注
蝶戀花·檻菊愁煙蘭泣露
檻【jiàn】菊愁煙蘭泣露,羅幕輕寒,燕子雙飛去。明月不諳離恨苦,斜光到曉穿朱戶。
昨夜西風凋碧樹,獨上高樓,望盡天涯路。欲寄彩箋兼尺素,山長水闊知何處?