zoukankan      html  css  js  c++  java
  • 07、应用程序资源(ApplicationResources 下)

    1、 Runtime Changes/Events:

            尽管一个应用程序正在运行,但它的 语言、缩放比、对比度或其他设置可能会改变。为了处理这些事件,应该注册事件监听器以侦听和处理相应的改变。这可以通过存储状态和重新划定特定的资源完成。 

           首先单击下面的按钮,显示当前上下文的资源。然后按照下面的方法改变语言设置:

           改变用户的首选语言:  Desktop Control Panel > Clock, Language,and Region > Language > Add a language that is supported by the app (eg. English, or Japanese) > Move new language to the top of the list.

    <Button x:Name="Scenario7Button_Show" Grid.Row="3"  Content="Show Message" 
    Margin="0,0,10,0" Click="Scenario7Button_Show_Click"/>

          逻辑代码:

     private void Scenario7Button_Show_Click(object sender, RoutedEventArgs e)
            {
                Button b = sender as Button;
                if (b != null)
                {
                  //MainResourceMap:  获取与当前正在运行的应用程序的主要包关联的 ResourceMap。
                    this.Scenario7TextBlock.Text = ResourceManager.Current.MainResourceMap.GetValue("Resources/string1").ValueAsString;
    
    
                 //QualifierValues :  获取可写入、可观测的所有受支持限定符的地图,按名称进行索引。
                 // MapChanged  事件:    在映射更改时发生。  
                    ResourceManager.Current.DefaultContext.QualifierValues.MapChanged += async (s, m) =>
                    {
                        await this.Scenario7TextBlock.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                        {
                             // GetValue()  :   返回资源中由默认上下文的资源标识符指定的最合适人选。                    
    
                                 this.Scenario7TextBlock.Text = ResourceManager.Current.MainResourceMap.GetValue("Resources/string1").ValueAsString;
                        });
                    };
                }
            }


    2、Application Languages:

          应用程序自动设置为最合适的语言(s)支持的应用程序和用户选择。应用程序没有必要调用所有的 APIs。资源、字体和 DOM 属性的所有设置都使用这种特定于应用程序的语言(s)。应用程序可以检索他们当前的语言(s)如果有必要与其他系统通过 ResourceContext  对象进行通信。一些应用程序可能还希望能够明确设置他们的应用程序语言(s),而不是用户的首选语言。

             应用程序可以通过调用来改变他们的应用程序语言# 10;
            注意,应用程序语言的设置,放在应用程序的 UI 上让用户来选择,而不是直接在应用的 canvas 中。

             选择下面的设置来显示相应的语言:

           .....

          本例的代码量较多,但是逻辑并不复杂。详细还是参照:ApplicationResource 工程下的 Scenario8.xaml 示例。

    3、Override Languages:

          本质上,3、 和 2、 类似,不详细解释,直接上代码:

      
    //在 前端页面分别设置一个 下拉框 和一个 按钮,让用户选择显示
    //哪个语言,按钮触发事件
    <ComboBox Margin="10, 10, 0, 13" SelectedIndex="0" SelectedValuePath="Name"  Name="Scenario9ComboBox" 
    Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="106" Height="34" >
                        <ComboBoxItem Name="en">English</ComboBoxItem>
                        <ComboBoxItem Name="ja">Japanese</ComboBoxItem>
                    </ComboBox>
      <Button x:Name="Scenario9Button_Show" Grid.Column="1" Content="Show Message" 
    Margin="0,0,10,0" Click="Scenario9Button_Show_Click"/>
    
    
    //显示用户的选择语言后,单击按钮后的结果
     <TextBlock Style="{StaticResource BasicTextStyle}" x:Name="Scenario9TextBlock" 
    FontSize="20" TextWrapping="Wrap" />
     private void Scenario9Button_Show_Click(object sender, RoutedEventArgs e)
            {
                Button b = sender as Button;
                if (b != null)
                {
                    var context = new ResourceContext();
    
                    var selectedLanguage = Scenario9ComboBox.SelectedValue;
                    if (selectedLanguage != null)
                    {
                        var lang = new List<string>();
                        lang.Add(selectedLanguage.ToString());
                        context.Languages = lang;  //获取或设置此上下文的语言限定符。
    
                           var resourceStringMap = ResourceManager.Current.MainResourceMap.GetSubtree("Resources");
    
                        //  返回资源中由提供的上下文的资源标识符指定的最合适候选。
                          this.Scenario9TextBlock.Text = resourceStringMap.GetValue("string1", context).ValueAsString;
                    }
                }
            }


    4、Multi-demensional  fallback:

         资源可以依赖于许多不同的方面,包括缩放比、语言和对比度。资源管理系统应当为当前上下文检索最合适的资源。

    资源限定符基于系统来评估优先权(例如,语言比缩放比更加重要)。应用程序开发人员被鼓励选择的资源应尽可能的适当。

    如果没有资源匹配的上下文,默认的资源是合格的资源选择。可以在编辑器中手动的编辑 .csproj文件来选择默认的资源。

            这个示例展示了一个对于不同的语言、缩放比 等的 列表的候选的字符串资源和命令组资源。在下拉框中的不同的上下文可以

    在不同的情形下匹配不同的资源。每个候选的资源都有额外的元数据,可以用于确定为什么它被选中。

    前段代码:

       <StackPanel   Grid.Row="2" >
                    <TextBlock Text="To view a resource that has been loaded inline in a different combination of qualifiers,
    choose the qualifier value below and click Show Message. &#10; &#10;
    " TextWrapping="Wrap"
    Style="{StaticResource BasicTextStyle}"/> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="150"/> <ColumnDefinition Width="150"/> <ColumnDefinition Width="150"/> <ColumnDefinition Width="150"/> <ColumnDefinition Width="150"/> </Grid.ColumnDefinitions> <TextBlock Grid.Column="0" Grid.Row="0" Text="Language"/> <ComboBox Grid.Column="0" SelectedValuePath="Tag" SelectedIndex="0" Grid.Row="1" Name="Scenario10ComboBox_Language" > <ComboBoxItem Tag="en-GB">English (United Kingdom)</ComboBoxItem> <ComboBoxItem Tag="en-US">English (United States)</ComboBoxItem> <ComboBoxItem Tag="fr-FR">French (France)</ComboBoxItem> <ComboBoxItem Tag="fr-CA">French (Canada)</ComboBoxItem> <ComboBoxItem Tag="de-DE">German (Germany)</ComboBoxItem> </ComboBox> <TextBlock Grid.Column="1" Grid.Row="0" Text="Scale"/> <ComboBox Grid.Column="1" SelectedIndex="0" SelectedValuePath="Content" Grid.Row="1" Name="Scenario10ComboBox_Scale" > <ComboBoxItem Content="100"></ComboBoxItem> <ComboBoxItem Content="140"></ComboBoxItem> <ComboBoxItem Content="180"></ComboBoxItem> </ComboBox> <TextBlock Grid.Column="2" Grid.Row="0" Text="Contrast"/> <ComboBox Grid.Column="2" SelectedIndex="0" Grid.Row="1" SelectedValuePath="Content" Name="Scenario10ComboBox_Contrast" > <ComboBoxItem Content="standard"></ComboBoxItem> <ComboBoxItem Content="white"></ComboBoxItem> <ComboBoxItem Content="black"></ComboBoxItem> </ComboBox> <TextBlock Grid.Column="3" Grid.Row="0" Text="HomeRegion"/> <ComboBox Grid.Column="3" SelectedValuePath="Tag" SelectedIndex="0" Grid.Row="1" Name="Scenario10ComboBox_HomeRegion" > <ComboBoxItem Tag="001">World</ComboBoxItem> <ComboBoxItem Tag="019">Americas</ComboBoxItem> <ComboBoxItem Tag="142">Asia</ComboBoxItem> <ComboBoxItem Tag="021">Northern America</ComboBoxItem> <ComboBoxItem Tag="419">Latin America and the Caribbean</ComboBoxItem> <ComboBoxItem Tag="840">United States of America</ComboBoxItem> </ComboBox> <Button x:Name="Scenario10Button_Show" Grid.Column="4" Grid.Row="1" Content="Show Message"
    Click="Scenario10Button_Show_Click" Margin="0,0,10,0"/> </Grid> </StackPanel>

    后端代码:

      private void Scenario10Button_Show_Click(object sender, RoutedEventArgs e)
            {
                Button b = sender as Button;
                if (b != null)
                {
                    var context = new ResourceContext();
    
                    var selectedLanguage = Scenario10ComboBox_Language.SelectedValue;
                    var selectedScale = Scenario10ComboBox_Scale.SelectedValue;
                    var selectedContrast = Scenario10ComboBox_Contrast.SelectedValue;
                    var selectedHomeRegion = Scenario10ComboBox_HomeRegion.SelectedValue;
    
                    if (selectedLanguage != null)
                    {
                        context.QualifierValues["language"] = selectedLanguage.ToString();
                    }
                    if (selectedScale != null)
                    {
                        context.QualifierValues["scale"] = selectedScale.ToString();
                    }
                    if (selectedContrast != null)
                    {
                        context.QualifierValues["contrast"] = selectedContrast.ToString();
                    }
                    if (selectedHomeRegion != null)
                    {
                        context.QualifierValues["homeregion"] = selectedHomeRegion.ToString();
                    }
    
                    Scenario10_SearchMultipleResourceIds(context, 
    new string[] { "LanguageOnly", "ScaleOnly", "ContrastOnly", "HomeRegionOnly", "MultiDimensional" }); } } void Scenario10_SearchMultipleResourceIds(ResourceContext context, string[] resourceIds) { this.Scenario10TextBlock.Text = ""; var dimensionMap = ResourceManager.Current.MainResourceMap.GetSubtree("dimensions"); foreach (var id in resourceIds) { NamedResource namedResource; //表示单个逻辑、命名的资源,如名为“Header1”的字符串资源。 if (dimensionMap.TryGetValue(id, out namedResource)) { var resourceCandidates = namedResource.ResolveAll(context); Scenario10_ShowCandidates(id, resourceCandidates); } } } void Scenario10_ShowCandidates(string resourceId, IReadOnlyList<ResourceCandidate> resourceCandidates) { // print 'resourceId', 'found value', 'qualifer info', 'matching condition' 
    string outText = "resourceId: dimensions\\" + resourceId + "\r\n"; int i = 0; foreach (var candidate in resourceCandidates) { var value = candidate.ValueAsString; outText += " Candidate " + i.ToString() + ":" + value + "\r\n"; int j = 0; foreach (var qualifier in candidate.Qualifiers) { var qualifierName = qualifier.QualifierName; var qualifierValue = qualifier.QualifierValue; outText += " Qualifer: " + qualifierName + ": " + qualifierValue + "\r\n"; outText += " Matching: IsMatch (" + qualifier.IsMatch.ToString() + ") "
    + "IsDefault (" + qualifier.IsDefault.ToString() + ")" + "\r\n"; j++; } i++; } this.Scenario10TextBlock.Text += outText + "\r\n"; }


    5、Working with webservices:

       当与一个web 服务进行通信时,需要指定一个单一的语言,一个被推荐使用的技术是创建一个代表url或查询参数的资源资源来确定发送到服务端的语言。这让资源管理系统能选择最合适的语言来发送。

         单击下面的按钮来决定 web 服务的 url:

     <Button x:Name="Scenario11Button_Show" Grid.Row="3" Content="Get Url" 
      Margin="0,0,10,0" Click="Scenario11Button_Show_Click"/>
    
    
     <TextBlock Style="{StaticResource BasicTextStyle}" x:Name="Scenario11TextBlock" 
    FontSize="20" TextWrapping="Wrap" />
    private void Scenario11Button_Show_Click(object sender, RoutedEventArgs e)
            {
                Button b = sender as Button;
                if (b != null)
                {
                    var resourceLoader = new ResourceLoader();
                    this.Scenario11TextBlock.Text = "http://api.contoso.com/Feed?lang=" + resourceLoader.GetString("webservicelang");
                }
            }
  • 相关阅读:
    C++中的命名空间
    [3D数学基础:图形与游戏开发]专栏前言
    Step01-题目申报
    《通用型云端物联网网关系统的设计与实现》
    博弈论题目总结(一)——简单组合游戏
    单纯形模板
    BZOJ 3434 [WC2014]时空穿梭 (莫比乌斯反演)
    BZOJ 3533 [SDOI2014]向量集 (线段树维护凸包)
    BZOJ 2161 布娃娃 (主席树)
    UOJ #86 mx的组合数 (数位DP+NTT+原根优化)
  • 原文地址:https://www.cnblogs.com/hebeiDGL/p/2686867.html
Copyright © 2011-2022 走看看