Nach einer ganztägigen Untersuchung dieses Kommentars Xaml-Lokalisierung: Verwenden von .resx-Ressourcen in Xaml ohne x: static Ich habe eine einfache Lösung gefunden, um mehrsprachige Unterstützung mit (eingebetteten Ressourcen oder referenzierter Assembly) * .resx-Dateien bereitzustellen. Seit Framework 4 gibt es eine Basisklasse namens DynamicObject zum Festlegen des dynamischen Verhaltens zur Laufzeit im Namespace System.Dynamic.
Ich habe folgenden ResourceLoader von der System.Dynamic.DynamicObject-Klasse abgeleitet:
public class ResourceLoader : DynamicObject
{
#region Fields ---------------------------------------------------------------
private const string DefaultResourcesSuffix = "Resource";
private ResourceManager _resourceMan;
private CultureInfo culture;
private readonly string _defaultAssemblyName;
private readonly Assembly _defaultAssembly;
private Assembly theAssembly;
private string resourcesSuffix;
private string assembly;
#endregion // Fields
#region Properties -----------------------------------------------------------
public string Assembly
{
get { return assembly; }
set
{
assembly = value;
theAssembly = System.Reflection.Assembly.Load(assembly);
_resourceMan = null;
}
}
public string ResourcesSuffix
{
get { return resourcesSuffix; }
set
{
resourcesSuffix = value;
_resourceMan = null;
}
}
public CultureInfo CurrentCulture
{
get { this.culture = this.culture ?? CultureInfo.InvariantCulture; return this.culture; }
set { this.culture = value; }
}
private ResourceManager ResourceManager
{
get
{
if (ReferenceEquals(_resourceMan, null))
{
ResourceManager temp = new ResourceManager(
string.Format("{0}.{1}", Assembly ?? _defaultAssemblyName, ResourcesSuffix ?? DefaultResourcesSuffix),
theAssembly ?? _defaultAssembly);
_resourceMan = temp;
}
return _resourceMan;
}
}
#endregion // Properties
#region Methods --------------------------------------------------------------
private object GetResource(string name, CultureInfo language)
{
if (language == null || language == CultureInfo.InvariantCulture)
return ResourceManager.GetObject(name);
return ResourceManager.GetObject(name, language);
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = GetResource(binder.Name, this.culture);
if (result != null && result.GetType() == typeof(System.Drawing.Bitmap))
{
System.Drawing.Bitmap currentBmp = result as System.Drawing.Bitmap;
currentBmp.MakeTransparent(System.Drawing.Color.Magenta);
BitmapSource src = Imaging.CreateBitmapSourceFromHBitmap(currentBmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
result = src;
}
return result == null ? false : true;
}
public void SwitchCulture(CultureInfo NewCulture)
{
this.culture = NewCulture;
}
#endregion // Methods
#region Constructors ---------------------------------------------------------
public ResourceLoader()
: this(CultureInfo.InvariantCulture, DefaultResourcesSuffix)
{ }
public ResourceLoader(CultureInfo InitCulture, string ResourceSuffix)
{
_defaultAssemblyName = GetType().Assembly.GetName().Name;
_defaultAssembly = GetType().Assembly;
this.culture = InitCulture;
this.resourcesSuffix = ResourceSuffix;
}
#endregion // Constructors
}
Sie können eine Instanz in xaml folgendermaßen erstellen:
<Application x:Class="ResourceFileAccessSample.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:ResourceFileAccessSample.ResourceBinding"
StartupUri="Window1.xaml" Startup="Application_Startup" >
<Application.Resources>
<src:ResourceLoader x:Key="resource" CurrentCulture="(Default)" ResourcesSuffix="Resource" />
</Application.Resources>
C # -Code:
public partial class Window1 : Window
{
private ResourceLoader res;
public Window1()
{
InitializeComponent();
this.res = (ResourceLoader)this.FindResource("resource");
this.LayoutRoot.DataContext = res;
}
private void btnSwichLanguage_Click(object sender, RoutedEventArgs e)
{
res.SwitchCulture(new CultureInfo("de"));
this.LayoutRoot.DataContext = null;
this.LayoutRoot.DataContext = res;
}
}
Jetzt ist es möglich, Zeichenfolgen und Bilder zu binden (Bilder werden in den WPF-Compilanten BitmapSource konvertiert:
<StackPanel Name="LayoutRoot" Orientation="Vertical">
<Label Name="lblText" Content="{Binding Path=rsName, Mode=OneWay}" HorizontalContentAlignment="Center" Margin="5" Padding="0" />
<Image Source="{Binding Path=AlignObjectsTop}" Height="16" Width="16" Margin="5" />
<Button Name="btnSwichLanguage" Content="Switch to de" Click="btnSwichLanguage_Click" MinHeight="25" Width="100" />
</StackPanel>