Lokalisierbare WPF-Anwendung – UICulture-Einstellungen führen zu Ressourcenproblemen
-
08-09-2020 - |
Frage
Ich möchte eine lokalisierbare WPF-Anwendung erstellen.Ich habe die Anweisungen in den Kommentaren der Datei AssemblyInfo.cs befolgt:
//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.
Nachdem ich dies getan habe, startet meine Anwendung nicht mehr.Ich verwende eine benutzerdefinierte App-Klasse:
namespace Namespace
{
public partial class App : Application
{
[STAThread()]
[SecurityPermission(SecurityAction.LinkDemand)]
public static void Main()
{
var app = new App();
app.InitializeComponent();
app.Run();
}
[DebuggerNonUserCode()]
public void InitializeComponent()
{
this.StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);
}
}
}
<ns:App
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ns="clr-namespace:Namespace">
</ns:App>
Bisher hat alles gut funktioniert.Ich gehe davon aus, dass zwischen den konfigurierten UICulture-Einstellungen und den für MainWindow.xaml angegebenen Einstellungen eine Art Diskrepanz besteht, aber ich weiß nicht, wie ich das beheben kann.
Lösung
Ich hatte einen ähnlichen Effekt;in der AssemblyInfo.cs die NeutralResourcesLanguage
Attribut benötigt die UltimateResourceFallbackLocation.Satellite
Parameter:
// Uncommenting the following line --> OK
[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
// Uncommenting the following line --> IOException: Cannot locate resource 'app.xaml'.
// [assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.MainAssembly)]
// Uncommenting the following line --> IOException: Cannot locate resource 'app.xaml'.
// [assembly: NeutralResourcesLanguage("en-US")]
Andere Tipps
Einige Hintergrundinformationen von: https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/wpf-globalization-and-localization-overview
Best Practices für die WPF-Lokalisierung
- Stellen Sie die ein
UltimateResourceFallback
Ort in AssemblyInfo.* Um die entsprechende Sprache für den Fallback anzugeben (zum Beispiel,,,,[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
). - Wenn Sie sich entscheiden, Ihre Quellsprache in die Hauptbaugruppe aufzunehmen, indem Sie das weglassen
<UICulture>
Tag in Ihrer Projektdatei, legen Sie das festUltimateResourceFallback
Ort als Hauptbaugruppe anstelle des Satelliten (zum Beispiel, zum Beispiel,[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.MainAssembly)]
)
.