简体   繁体   中英

WPF localization with dynamic resource doesn't work

I made a custom UI wix bootstrapper as a WPF .net class library. I wanted to change the setup language in realtime according to the user choice by using a MergedDictionaries and DynamicRessource.

But the issue I encounter is that the string doesn't show on the UI and it's not an uri path error because I can access it with the code behind.

<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>

The code used to access the string ressource in the view

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

The StringResources.fr-FR.xaml file

<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>
  

Modify your window markup to include the merged resource dictionary in the Resources dictionary directly:

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

Or merge the ResourceDictionary at application level in App.xaml :

<Application x:Class="WpfApp1.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="pack://application:,,,/LanguageSelector;component/Langues/StringResources.fr-FR.xaml"/>
            </ResourceDictionary.MergedDictionaries>
       </ResourceDictionary>
    </Application.Resources>
</Application>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM