簡體   English   中英

本地化WPF應用程序不起作用?

[英]Localising a WPF application doesn't work?

我必須在這里遺漏一些東西。

我在VS2015中創建了一個全新的WPF應用程序。 我創建了一個資源'String1'並將值設置為'fksdlfdskfs'。

我更新默認的MainWindow.xaml.cs,以便構造函數具有:

    public MainWindow()
    {
        InitializeComponent();
        this.Title = Properties.Resources.String1;
    }

並運行該應用程序,它工作正常,我的窗口標題是fksdlfdskfs。

在AssemblyInfo.cs文件中,我看到以下注釋:

//In order to begin building localizable applications, set 
//<UICulture>CultureYouAreCodingWith</UICulture> in your .csproj file
//inside a <PropertyGroup>.  For example, if you are using US english
//in your source files, set the <UICulture> to en-US.  Then uncomment
//the NeutralResourceLanguage attribute below.  Update the "en-US" in
//the line below to match the UICulture setting in the project file.

//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]

所以我將以下內容添加到我的WpfApplication5.csproj文件中並在VS中重新加載項目:

<UICulture>en-US</UICulture>

然后在AssemblyInfo.cs中取消注釋以下行:

[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]

如果我現在去運行應用程序,應用程序不再運行,我在讀取資源的行上得到以下異常:

System.Resources.MissingManifestResourceException:找不到適合指定文化或中性文化的任何資源。 確保在編譯時將“WpfApplication5.Properties.Resources.en-US.resources”正確嵌入或鏈接到程序集“WpfApplication5”中,或者所有所需的附屬程序集都是可加載和完全簽名的。

如果我改變UltimateResourceFallbackLocation.SatelliteUltimateResourceFallbackLocation.MainAssembly AssemblyInfo.cs文件中,我得到下面的異常,而不是:

System.IO.IOException:找不到資源'mainwindow.xaml'

我做錯了什么或我錯過了什么?

你沒有被迫使用代碼來進行本地化,你可以簡單地使用x:Static標記擴展來綁定到靜態字段:

<Window
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:properties="clr-namespace:SandBox.Properties"
    Title="{x:Static properties:Resources.TitleSandbox}">

</Window>

只需確保您的資源文件訪問修飾符設置為公共

屏幕資源文件

您獲得的錯誤消息通常意味着您沒有Resource.en-US.resx文件,因為[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]告訴您的應用使用en en-US資源file作為默認源。 如果要快速排除錯誤,請添加名為Resources.en-US.resx的文件

我個人做的本地化WPF應用程序是:

  • 我按原樣保留AssemblyInfo.cs ,這意味着Resource.resx (沒有語言ID)文件將是默認文件(通常是en-US)
  • 我在默認旁邊創建了額外的Resource.{id}.resx文件,如下所示: 樣品

    它通常與Resource.resx相同,但在匹配語言中進行翻譯

  • 我在啟動時(通常在App.xaml.cs )使用用戶可設置的語言ID強制文化,以便用戶可以更改應用程序語言:
// language is typically "en", "fr" and so on
var culture = new CultureInfo(language);
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;
// You'll need to restart the app if you do this after application init

暫無
暫無

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

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