You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF动态资源本地化失效:自定义Wix引导程序UI字符串不显示

WPF类库中Wix引导程序多语言资源无法显示的问题解决

我将带自定义UI的Wix引导程序实现为WPF .NET类库,计划通过MergedDictionaries和DynamicResource根据用户选择实时切换安装语言,但遇到字符串资源无法在UI上显示的问题。已排除URI路径错误,因为后台代码可以正常访问该资源。

相关代码如下:

Window资源定义

<Window.Resources>
    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    <ResourceDictionary x:Key="Application">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/LanguageSelector;component/Langues/StringResources.fr-FR.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

UI访问资源代码

<TextBlock Text="{DynamicResource Setup}" Margin="10" FontSize="16" 
           Visibility="{Binding Path=LanguageSelectorUIEnabled, Converter={StaticResource BooleanToVisibilityConverter}}" />

语言资源文件(StringResources.fr-FR.xaml)

<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="Setup">Choissisez la langue du gestionnaire d'installation</system:String>

问题根源

  1. 嵌套资源字典未加入全局查找链:Window.Resources中定义的ResourceDictionary设置了x:Key="Application",导致该字典不在全局资源查找范围内,DynamicResource无法自动找到其中的资源。
  2. 资源文件语法错误:StringResources.fr-FR.xaml中的ResourceDictionary标签未闭合,会导致资源加载失败。

修复步骤

1. 调整Window资源结构

去掉嵌套字典的x:Key,将合并字典直接放在根资源字典中,让DynamicResource能正常查找:

<Window.Resources>
    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/LanguageSelector;component/Langues/StringResources.fr-FR.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</Window.Resources>

如果需要保留带Key的嵌套字典,可在后台代码中将其合并到全局资源:

public MainWindow()
{
    InitializeComponent();
    var appDict = (ResourceDictionary)Resources["Application"];
    Resources.MergedDictionaries.Add(appDict);
}

2. 修复语言资源文件的语法错误

闭合ResourceDictionary标签:

<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="Setup">Choissisez la langue du gestionnaire d'installation</system:String>
</ResourceDictionary>

额外验证与切换逻辑

  • 确保语言资源文件的生成操作设置为Resource(右键文件→属性→生成操作)。
  • 实时切换语言时,需移除旧的语言字典并添加新字典,示例代码:
// 移除旧语言字典
var oldLangDict = Resources.MergedDictionaries.FirstOrDefault(d => 
    d.Source?.OriginalString.Contains("StringResources.") == true);
if (oldLangDict != null)
{
    Resources.MergedDictionaries.Remove(oldLangDict);
}
// 添加新语言字典
var newLangDict = new ResourceDictionary
{
    Source = new Uri("pack://application:,,,/LanguageSelector;component/Langues/StringResources.zh-CN.xaml", UriKind.RelativeOrAbsolute)
};
Resources.MergedDictionaries.Add(newLangDict);

内容的提问来源于stack exchange,提问作者Bob John

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 01:55:20