老周的部落格專欄:http://blog.csdn.net/tcjiaan
轉載請註明原作者和出處。
上一篇文章中,我們吹了一下資源和本地化,同時也做了一個執行個體,本文我們再深入探索一下資源限定符和資源路徑的映射。這兩個玩意兒也許我們在實際開發中並不十分關注,不過,瞭解一下,還是有好處的。
這兩個名詞看起來就抽象,或者,我們會感覺到,從文字描述無法理解它們,那麼,老規矩,我們還是用實驗來看看是否能將抽象的概念形象化。
1、啟動VS,建立一個Modern風格的應用程式項目(也就前面說過的板磚風格)。
2、在“方案總管”中的項目節點上右擊,從捷徑功能表中依次選擇“添加”-“建立項”,在模板列表中找到資源檔(.resw),檔案名稱按預設Resources即可,確定。
3、在剛才建立的資源檔中,隨便輸入一些資源,
4、儲存並關閉資源檔,另外,在項目中建立兩個空白頁面,分別為PageQt.xaml和PageMaps.xaml,現在,你的解決方案結構應該類似所示。
(1)開啟PageQt.xaml,介面布局參考下面的XAML。
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <TextBlock Name="tb" FontSize="20" TextWrapping="Wrap" Margin="3"/> </Grid>
(2)切換到PageQt.xaml.cs,C#代碼如下所示。
using System;using System.Collections.Generic;using System.IO;using System.Linq;using Windows.Foundation;using Windows.Foundation.Collections;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Controls.Primitives;using Windows.UI.Xaml.Data;using Windows.UI.Xaml.Input;using Windows.UI.Xaml.Media;using Windows.UI.Xaml.Navigation;// 引入以下命名空間using Windows.ApplicationModel.Resources;using Windows.ApplicationModel.Resources.Core;namespace MyApp{ /// <summary> /// 可用於自身或導航至 Frame 內部的空白頁。 /// </summary> public sealed partial class PageQt : Page { public PageQt() { this.InitializeComponent(); this.Loaded += PageQt_Loaded; } void PageQt_Loaded(object sender, RoutedEventArgs e) { ResourceContext context = ResourceManager.Current.DefaultContext; string resultStr = string.Empty; foreach (var item in context.QualifierValues) { resultStr += string.Format("{0} => {1}", item.Key, item.Value); resultStr += "\n"; } this.tb.Text = resultStr; } }}
(3)儲存,接著開啟PageMaps.xaml,XAML如下所示。
<Page x:Class="MyApp.PageMaps" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:MyApp" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <TextBlock Name="tb" FontSize="20" TextWrapping="Wrap" Margin="3"/> </Grid></Page>
切換到C#程式碼檢視,處理代碼如下面清單所示:
using System;using System.Collections.Generic;using System.IO;using System.Linq;using Windows.Foundation;using Windows.Foundation.Collections;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Controls.Primitives;using Windows.UI.Xaml.Data;using Windows.UI.Xaml.Input;using Windows.UI.Xaml.Media;using Windows.UI.Xaml.Navigation;// 引用以下命名空間using Windows.ApplicationModel.Resources;using Windows.ApplicationModel.Resources.Core;namespace MyApp{ public sealed partial class PageMaps : Page { public PageMaps() { this.InitializeComponent(); this.Loaded += (sender, args) => { string str = ""; foreach (var item in ResourceManager.Current.AllResourceMaps) { str += "-------------------- " + item.Key + " --------------------\n"; foreach (var x in item.Value) { str += string.Format("{0} => {1}\n", x.Key, x.Value.Uri); } str += "\n\n"; } this.tb.Text = str; }; } }}
5、現在回到MainPage.xaml,把頁面根Grid分為兩列,左邊放一個ListBox,右邊放一個Frame,相信你也猜到用來幹啥的,對,就是用Frame來導航顯示前面我們做的兩個頁面,一個顯示資源限定符資訊,另一個顯示路徑映射。
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <Grid.ColumnDefinitions> <ColumnDefinition Width="150"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <ListBox Name="lb" Grid.Column="0" SelectionChanged="onSelectionChanged"> <ListBoxItem>資源限定符</ListBoxItem> <ListBoxItem>資源地圖</ListBoxItem> </ListBox> <Frame x:Name="frameRight" Grid.Column="1" Margin="2"/> </Grid>
[C# Code]
using System;using System.Collections.Generic;using System.IO;using System.Linq;using Windows.Foundation;using Windows.Foundation.Collections;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Controls.Primitives;using Windows.UI.Xaml.Data;using Windows.UI.Xaml.Input;using Windows.UI.Xaml.Media;using Windows.UI.Xaml.Navigation;// “空白頁”項目範本在 http://go.microsoft.com/fwlink/?LinkId=234238 上有介紹namespace MyApp{ /// <summary> /// 可用於自身或導航至 Frame 內部的空白頁。 /// </summary> public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); } /// <summary> /// 在此頁將要在 Frame 中顯示時進行調用。 /// </summary> /// <param name="e">描述如何訪問此頁的事件數目據。Parameter /// 屬性通常用於配置頁。</param> protected override void OnNavigatedTo(NavigationEventArgs e) { lb.SelectedIndex = 0; } private void onSelectionChanged(object sender, SelectionChangedEventArgs e) { ListBox lb = sender as ListBox; if (lb!=null) { int index = lb.SelectedIndex; switch (index) { case 0: this.frameRight.Navigate(typeof(PageQt)); break; case 1: this.frameRight.Navigate(typeof(PageMaps)); break; default: this.frameRight.Navigate(typeof(PageQt)); break; } } } }}
6、開啟資訊清單檔Package.appxmanifest,然後切換到“打包”選項卡,把包名改一下,改成一個好看一點,方便查看的名字。後面有用。
現在,運行一下。
從第一張中,我們可以大概知道什麼是資源限定符,上一篇文章中我們的例子,實現簡/繁體中文切換,我們用到了限定符中的一種——語言標記。我們看到,限定符有:
Language:當前應用首選的語言;
Contrast:對比。
Scale:縮放比例。
HomeRegion:地區。
And so on.
如果你想瞭解更多有關限定符的東東,可以看看官方的文檔。
http://msdn.microsoft.com/zh-cn/library/windows/apps/hh965372.aspx
這東西沒什麼技巧可言,你就按照文檔說的去做就行了。
再來看看路徑映射,前面我們修改包名,就是為了在這裡好查看,
不知道各位在上面的中發現了什麼規律?
1、資源路徑是以ms-resource:// 打頭的。
2、應用程式所認為的資源不僅僅是.resw檔案,看,幾乎把我們項目中的所有檔案都列出來了。
3、那麼,我們在引用某項資源時,是按key來引用的,也就是圖中“=>”左邊的內容。
4、重點關注一下Resources.resw的映射,我們看到,Resources.resw的引用名為Resources,這個名字是預設的,還記得上一篇文章中的例子嗎?我們執行個體化ResourceLoader時,是調用了無參數的建構函式,那是因為Resources是預設值,如果我們添加的資源檔是abc.resw,那麼我們在執行個體化ResourceLoader時,就不能用無參建構函式了,而要傳遞一個引用名,按照上面我們實驗得出的規律,引用名應當是abc。
繼續回到,在Resources下的兩個資源項也被映射到不同的URI,即Resources/Key1和Resources/Key2,所以,以後要引用資源,你應該懂得怎麼做了。
ResourceMap類有個GetSubtree方法,它可以返回指定引用的子映射,比如,中,如果調用GetSubtree("Resources"),這樣,返回的Map就只剩下Key1和Key2了,就是我們要找的資源項,然後在返回的ResourceMap上再調用GetValue("Key1"),就能取到資源了。
下面再做執行個體,看看如何從ResourceMap映射中取出資源項的值。
1、建立項目。
2、在MainPage.xaml中完成布局。
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <StackPanel Margin="25"> <TextBlock FontSize="20" Text="第一個值:"/> <TextBlock FontSize="24" Name="tb1"/> <TextBlock FontSize="20" Margin="0,20,0,0" Text="第二個值:"/> <TextBlock FontSize="24" Name="tb2"/> </StackPanel> </Grid>
3、切換到C#程式碼檢視。
protected override void OnNavigatedTo(NavigationEventArgs e) { ResourceMap map = ResourceManager.Current.MainResourceMap.GetSubtree("Resources"); tb1.Text = map.GetValue("v1").ValueAsString; tb2.Text = map.GetValue("v2").ValueAsString; }
然後,運行程式,查看效果。