gpt4 book ai didi

c# - 多语言 wpf 应用程序

转载 作者:可可西里 更新时间:2023-11-01 08:51:17 29 4
gpt4 key购买 nike

我有一个 WPF 应用程序(英文版),我想让用户选择不同的语言。我已经阅读了一些在运行时应用程序中更改语言的可能性,但我只想在安装时选择一种语言并且永远不会更改它。

您认为最快和最简单的方法是开发不同版本的程序(仅更改文本语言)并让用户在安装过程中选择其中一种吗?可能重复代码只改变文本框或标签不是很优雅,但请注意我已经用英语完成了应用程序并且我不需要在运行时更改语言。

最佳答案

您可以按照以下步骤操作:

  1. 创建资源文件

    将此文件 StringResources.xaml 添加到 Resources 目录。这是一个例子:

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

    <system:String x:Key="close">Close</system:String>
    </ResourceDictionary>

    您可以创建多个文件,每种语言一个。

  2. 添加资源(启动应用程序时调用)

    private void SetLanguageDictionary()
    {
    ResourceDictionary dict = new ResourceDictionary();
    switch (Thread.CurrentThread.CurrentCulture.ToString())
    {
    case "en-US":
    dict.Source = new Uri("..\\Resources\\StringResources.xaml", UriKind.Relative);
    break;
    case "fr-CA":
    dict.Source = new Uri("..\\Resources\\StringResources.fr-CA.xaml", UriKind.Relative);
    break;
    default :
    dict.Source = new Uri("..\\Resources\\StringResources.xaml",UriKind.Relative);
    break;
    }
    this.Resources.MergedDictionaries.Add(dict);
    }
  3. 像这样使用资源 -

    <Button      
    x:Name="btnLogin"
    Click="btnLogin_Click"
    Content="{DynamicResource close}"
    Grid.Row="3"
    Grid.Column="0"
    Padding="10" />

来源:https://www.codeproject.com/Articles/123460/Simplest-Way-to-Implement-Multilingual-WPF-Applica

关于c# - 多语言 wpf 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11327840/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com