• WPF页面 全球化和本地化


    传统的 新建.resx类型的文件中,然后利用ResourceManager来得到相应资源并根据当地的CultureInfo来给界面文本赋值。

    WPF

    新建一个文件夹 Language

    新建2个资源字典。

    DefaultLanguage.xaml

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:sys="clr-namespace:System;assembly=mscorlib"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <sys:String x:Key="OK">
            OK
        </sys:String>
    
        <sys:String x:Key="Cancel">
            Cancel
        </sys:String>
    </ResourceDictionary>

    zh-CN.xaml

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:sys="clr-namespace:System;assembly=mscorlib">
    
        <sys:String x:Key="OK">
            确定
        </sys:String>
    
        <sys:String x:Key="Cancel">
            取消
        </sys:String>
    
    </ResourceDictionary>

    App.xaml 引入路径

    <Application x:Class="WpfApplication1.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="MainWindow.xaml">
        
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="LanguageDefaultLanguage.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
    
        </Application.Resources>
    </Application>

    App.cs代码:

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Windows;
    using System.Globalization;
    
    namespace WpfApplication1
    {
        /// <summary>
        /// App.xaml 的交互逻辑
        /// </summary>
        public partial class App : Application
        {
            protected override void OnStartup(StartupEventArgs e)
            {
                base.OnStartup(e);
                LoadLanguage();
            }
    
            private void LoadLanguage()
            {
                CultureInfo currentCultureInfo = CultureInfo.CurrentCulture;
                ResourceDictionary langRd = null;
    
                try
                {
                    langRd = Application.LoadComponent(new Uri(string.Format("{0}{1}.xaml", "Language\", currentCultureInfo.Name), UriKind.Relative)) as ResourceDictionary;
                }
                catch
                {
                }
    
                if (langRd != null)
                {
                    if (this.Resources.MergedDictionaries.Count > 0)
                    {
                        this.Resources.MergedDictionaries.Clear();
                    }
                    this.Resources.MergedDictionaries.Add(langRd);
                }
    
            }
        }
    }

    页面使用:

                <Button Content="{DynamicResource OK}"/>
                <Button Content="{DynamicResource Cancel}"/>
  • 相关阅读:
    c++ heap学习
    超长正整数相加
    Search Insert Position
    strcpy与strcat函数原型
    C++基本数据类型占字节数
    详解指针的指针
    Google 超分辨率技术 RAISR
    elementui resetFields方法重置表单失败
    VS 点击文件自动定位到解决方案资源管理器中文件所在目录位置
    mybatis中LIKE模糊查询的几种写法以及注意点
  • 原文地址:https://www.cnblogs.com/y112102/p/3723568.html
Copyright © 2020-2023  润新知