[源碼下載] 背水一戰 Windows 10 (9) - 資源: 資源限定符概述, 資源限定符示例 作者:webabcd介紹背水一戰 Windows 10 之 資源 資源限定符概述 資源限定符示例 示例1、資源限定符概述Resource/Qualifiers/Summary.xaml Resourc ...
背水一戰 Windows 10 (9) - 資源: 資源限定符概述, 資源限定符示例
作者:webabcd
介紹
背水一戰 Windows 10 之 資源
- 資源限定符概述
- 資源限定符示例
示例
1、資源限定符概述
Resource/Qualifiers/Summary.xaml
<Page x:Class="Windows10.Resource.Qualifiers.Summary" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows10.Resource.Qualifiers" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="10 0 10 10"> <TextBlock Name="lblMsg" Margin="0 10 0 0" /> </StackPanel> </Grid> </Page>
Resource/Qualifiers/Summary.xaml.cs
/* * qualifiers - 限定符,在 uwp 中支持通過限定符來命名資源。限定符用於標識某個資源文件使用場景的上下文 * * 本例用於演示如何獲取系統支持的全部限定符 * 關於限定符的規則及示例參見 /Resource/Qualifiers/Demo.xaml */ using System; using Windows.Foundation.Collections; using Windows.Graphics.Display; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; namespace Windows10.Resource.Qualifiers { public sealed partial class Summary : Page { public Summary() { this.InitializeComponent(); this.Loaded += Summary_Loaded; } private void Summary_Loaded(object sender, RoutedEventArgs e) { // 列舉出系統支持的全部限定符,及其對應的值 IObservableMap<string, string> qualifiers = Windows.ApplicationModel.Resources.Core.ResourceManager.Current.DefaultContext.QualifierValues; foreach (var qualifier in qualifiers) { lblMsg.Text += string.Format("{0}: {1}", qualifier.Key, qualifier.Value); lblMsg.Text += Environment.NewLine; } lblMsg.Text += Environment.NewLine; // 常用的有:Scale, DeviceFamily, Language, TargetSize, 其他的都不常用 // 獲取當前的縮放比例 string scale; Windows.ApplicationModel.Resources.Core.ResourceManager.Current.DefaultContext.QualifierValues.TryGetValue("Scale", out scale); lblMsg.Text += "縮放比例: " + scale; lblMsg.Text += Environment.NewLine; // 獲取當前的縮放比例(Windows.Graphics.Display.ResolutionScale 枚舉) lblMsg.Text += "ResolutionScale: " + DisplayProperties.ResolutionScale.ToString(); lblMsg.Text += Environment.NewLine; // 獲取當前的設備類型 string deviceFamily; Windows.ApplicationModel.Resources.Core.ResourceManager.Current.DefaultContext.QualifierValues.TryGetValue("DeviceFamily", out deviceFamily); lblMsg.Text += "設備類型: " + deviceFamily; lblMsg.Text += Environment.NewLine; // 獲取當前的語言類型 string language; Windows.ApplicationModel.Resources.Core.ResourceManager.Current.DefaultContext.QualifierValues.TryGetValue("Language", out language); lblMsg.Text += "語言類型: " + language; lblMsg.Text += Environment.NewLine; } } }
2、資源限定符示例
Resource/Qualifiers/Demo.xaml
<Page x:Class="Windows10.Resource.Qualifiers.Demo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows10.Resource.Qualifiers" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="10 0 10 10"> <TextBlock Name="lblMsg" Margin="5" /> <!-- 限定符用於標識某個資源文件使用場景的上下文,本例通過 DeviceFamily 和 Scale 兩種限定符來演示實際效果(關於系統支持的全部限定符參見 /Qualifiers/Summary.xaml),規則總結如下 1、命名規則 a) 文件夾式: folder/qualifiername-value/qualifiername-value/filename.ext 或者 folder/qualifiername-value/qualifiername-value_qualifiername-value/filename.ext 等 b) 文件名式: folder/filename.qualifiername-value_qualifiername-value.ext 等(多個限定符之間用“_”分隔) c) 文件夾式和文件名式可以混合使用 2、調用規則: 直接引用 folder/filename.ext,系統將自動根據限定符指定的上下文做匹配 3、無論是資源名,還是限定符都不區分大小寫 4、同一個資源的限定符不能重覆,否則編譯報錯。比如 folder/filename.scale-100.png 和 folder/scale-100/filename.png 其實是同名限定符資源,不能共存 5、對於非 scale 限定符資源,如果匹配不到,系統則會去調用對應的無限定符資源 6、對於 scale 限定符資源,只要有一個 scale 資源就不會去調用對應的無限定符資源。當無法精確匹配時,系統先去找臨近的更大比例的,找不到的話再按從大到小的順序找小比例的 7、語言限定符有一個特殊性,其限定符名稱可以是 language 或 lang,用文件夾式的話是不需要限定符名稱的。比如文件名式 filename.language-en-US.png 或 filename.lang-en-US.png 對應的文件夾式為 en-US/filename.png 另外:限定符 TargetSize 用於限定圖標的大小,不能和 Scale 共存 1、TargetSize 主要用於桌面 Windows 資源管理器中顯示的文件類型關聯圖標或協議圖標 2、TargetSize 限定的是一個正方形圖標,其值的單位是像素,類似 filename.targetsize-16.png, filename.targetsize-32.png 等 3、當無法精確匹配時,系統先去找臨近的更大像素的,找不到的話再按從大到小的順序找小像素的 4、去 Package.appxmanifest 的“可見資產”看看,標識為“比例”的對應的是 Scale 限定符,標識為“目標大小”的對應的是 TargetSize 限定符 --> <StackPanel Orientation="Horizontal"> <Image Source="/Resource/Qualifiers/myImage1.png" Width="100" Height="100" HorizontalAlignment="Left" Margin="5" /> <Image Source="/Resource/Qualifiers/myImage2.png" Width="100" Height="100" HorizontalAlignment="Left" Margin="5" /> </StackPanel> <StackPanel Orientation="Horizontal" Margin="0 10 0 0"> <Image Source="/Resource/Qualifiers/myImage3.png" Width="100" Height="100" HorizontalAlignment="Left" Margin="5" /> <Image Source="/Resource/Qualifiers/myImage4.png" Width="100" Height="100" HorizontalAlignment="Left" Margin="5" /> </StackPanel> </StackPanel> </Grid> </Page>
Resource/Qualifiers/Demo.xaml.cs
/* * 本例用於演示限定符的實際效果 */ using System; using Windows.Graphics.Display; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; namespace Windows10.Resource.Qualifiers { public sealed partial class Demo : Page { public Demo() { this.InitializeComponent(); this.Loaded += Demo_Loaded; } private void Demo_Loaded(object sender, RoutedEventArgs e) { // 獲取當前的縮放比例 string scale; Windows.ApplicationModel.Resources.Core.ResourceManager.Current.DefaultContext.QualifierValues.TryGetValue("Scale", out scale); lblMsg.Text += "縮放比例: " + scale; lblMsg.Text += Environment.NewLine; // 獲取當前的縮放比例(Windows.Graphics.Display.ResolutionScale 枚舉) lblMsg.Text += "ResolutionScale: " + DisplayProperties.ResolutionScale.ToString(); lblMsg.Text += Environment.NewLine; // 獲取當前的設備類型 string deviceFamily; Windows.ApplicationModel.Resources.Core.ResourceManager.Current.DefaultContext.QualifierValues.TryGetValue("DeviceFamily", out deviceFamily); lblMsg.Text += "設備類型: " + deviceFamily; lblMsg.Text += Environment.NewLine; } } }
註:本例的資源文件結構
|Resource |--Qualifiers |----deviceFamily-desktop |------scale-100 |--------MyImage4.png |----deviceFamily-mobile |------MyImage4.scale-100.png |----scale-100 |------MyImage1.png |----scale-140 |------MyImage1.png |----scale-180 |------MyImage1.png |----MyImage1.png |----MyImage2.png |----MyImage2.scale-100.png |----MyImage2.scale-140.png |----MyImage2.scale-180.png |----MyImage3.DeviceFamily-Desktop_scale-100.png |----MyImage3.DeviceFamily-Mobile_scale-100.png |----MyImage3.png |----MyImage4.png
OK
[源碼下載]