WPF程序国际化

1.创建相应的xaml文件

 

复制代码
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sp="http://schemas.microsoft.com/netfx/2007/xaml/presentation"
    xmlns:sys="clr-namespace:System;assembly=mscorlib">


    <sp:ImageSource x:Key="Logo">
        logo url
    </sp:ImageSource>

    <sys:String x:Key="Msg">
       string
    </sys:String>

</ResourceDictionary>
复制代码

 

 

2.配置App.xaml

<Application.Resources>
<ResourceDictionary>
<!-- 这个节点就是配置默认语言的 -->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Languages\en_US.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>

 

 

3.程序中如果要修改,则可以使用如下代码

复制代码
                ResourceDictionary langRd = null;
                try
                {
                    //base on language config file
                    langRd = Application.LoadComponent(new Uri(@"Languages\" + languageName + ".xaml", UriKind.Relative)) as ResourceDictionary;
                }
                catch (Exception e2)
                {
                    MessageBox.Show(e2.Message);
                }

                if (langRd != null)
                {
                    //clear base style
                    //if (this.Resources.MergedDictionaries.Count > 0)
                    //{
                    //    this.Resources.MergedDictionaries.Clear();
                    //}
                    this.Resources.MergedDictionaries.Add(langRd);
                }
复制代码

 

 4.如果要单独取得某个配置项目,则可以在程序中使用如下代码:

using System.Windows
(string)TryFindResource("Msg");

 

posted @   风来风往风伤(TiAmo)  阅读(293)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示