日期:2014-05-17  浏览次数:20770 次

新时尚Windows8开发(7):资源限定符和资源映射

老周的博客专栏: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) =>
                {