zoukankan      html  css  js  c++  java
  • 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}"/>
  • 相关阅读:
    在Ubuntu 桌面版 12.04 LTS安装并运行SSH
    将Tp-link无线路由器桥接到Dlink无线路由器上
    如何解决Win7将任务栏程序自动分组的困扰
    安装Ubuntu 桌面版 12.04 LTS 过程之记录
    #lspci | grep Eth
    做技术不能人云亦云
    如何使用FF的Firebug组件中的net工具查看页面元素加载消耗时间
    在Fedora8上安装使用ActiveMQ5.8
    多态继承中的内存图解 && 多态中的对象变化的内存图解
    孔子装爹案例_帮助理解多态的成员访问特点及转型
  • 原文地址:https://www.cnblogs.com/y112102/p/3723568.html
Copyright © 2011-2022 走看看