簡體   English   中英

具有Visual Studio Designer中資源的多語言WPF應用程序

[英]Multilanguage wpf application with resources in Visual Studio Designer

這是我的問題:我有多語言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>

然后我在Designer中擁有此資源,但是我無法設置多語言。

使用多語言應用程序的設計器中的可見資源是否有可能? 也許在打開應用程序時更改了app.xaml文件?

您的方法正確。

  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. 從資源獲取本地化的值時,必須使用DynamicResources而不是StaticResources:

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

這個對我有用。 希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM