【发布时间】:2020-11-16 22:54:05
【问题描述】:
我正在使用 XAML 文件构建我的语言系统。
我的代码如下:
es.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyAppTest"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<system:String x:Key="Hello">Hola</system:String>
</ResourceDictionary>
en.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyAppTest"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<system:String x:Key="Hello">Hello</system:String>
</ResourceDictionary>
App.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Name="en" Source="mylang/en.xaml"/>
<ResourceDictionary x:Name="es" Source="mylang/es.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Main.xaml
<TextBlock Text="{DynamicResource Hello}" />
<ComboBox>
<ComboBoxItem Content="English" />
<ComboBoxItem Content="Spanish" />
</ComboBox>
如何根据ComboBox中选择的语言读取语言XAML文件?
【问题讨论】:
-
问:因此,您希望在程序执行时动态更改语言,以响应更改 ComboBox 中的选择。对吗?
-
@paulsm4 是的,就是这样
-
这能回答你的问题吗? Change language at runtime in C# winform
-
@paulsm4 我相信不是因为这个链接使用 resx 文件而且我使用的是 xaml
-
Dude - 关键是您的 callback 需要修改 CurrentUICulture 属性,调用 ComponentResourceManager.ApplyResources 等。您的问题的答案涉及 C# 代码 i>,不仅仅是 XAML 标记!你的 ComboBox 有一个事件处理程序,不是吗?