1

これは私の問題です: 2 つの異なるファイルにリソースを持つ多言語 WPF アプリケーションがあります。今、次のように app.xaml.cs で適切なものを選択します。

var dict = new ResourceDictionary();
switch (Thread.CurrentThread.CurrentCulture.ToString())
{
    case "de-DE":
        dict.Source = new Uri("pack://application:,,,/Resources;component/StringResources.de-DE.xaml", UriKind.Absolute);
        break;
    default:
        dict.Source = new Uri("pack://application:,,,/Resources;component/StringResources.xaml", UriKind.Absolute);
        break;
}
Resources.MergedDictionaries.Add(dict);

すべて正常に動作しますが、VisualStudio Designer にそのリソースが表示されません。

一方、App.xaml ファイルで ResourceDictionary を次のように定義すると、次のようになります。

<Application x:Class="Ampe.UI.Views.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Exit="App_OnExit" ShutdownMode="OnMainWindowClose">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/Resources;component/StringResources.de-DE.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

次に、デザイナーにこのリソースがありますが、多言語を設定できません。

多言語アプリケーションを使用してデザイナーでリソースを表示する可能性はありますか? アプリケーションの電源が入っている間に app.xaml ファイルを変更したのでしょうか。

4

1 に答える 1

2

あなたは正しい道を進んでいます。

  1. 新しい辞書を追加する前に、アプリケーションにマージされた辞書をクリアすることをお勧めします。

        Resources.MergedDictionaries.Clear();
        var dict = new ResourceDictionary();
        switch (Thread.CurrentThread.CurrentCulture.ToString())
        {
            case "de-DE":
                dict.Source = new Uri("pack://application:,,,/Resources;component/StringResources.de-DE.xaml", UriKind.Absolute);
                break;
            default:
                dict.Source = new Uri("pack://application:,,,/Resources;component/StringResources.xaml", UriKind.Absolute);
                break;
        }
        Resources.MergedDictionaries.Add(dict);
    
  2. app.xaml は次のようになります。

    <Application x:Class="Ampe.UI.Views.App"
    
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Exit="App_OnExit" ShutdownMode="OnMainWindowClose">
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="pack://application:,,,/Resources;component/StringResources.de-DE.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    </Application>
    
  3. リソースからローカライズされた値を取得する場合、StaticResources の代わりに DynamicResources を使用する必要があります。

    <TextBlock Text="{DynamicResource MyString}" />
    

わたしにはできる。それが役に立てば幸い。

于 2015-07-23T10:39:37.987 に答える