Get values ​​from * .resx files in XAML

Is it possible to add some value from the resource file directly to the XAML markup? Or for localization, we always need to do something similar in the * .cs file:

txtMessage.Text = Messages.WarningUserMessage; 

Where Messages is the resource and txtMessage is the TextBlock.

+50
c # localization xaml resx
May 27 '10 at 7:57 a.m.
source share
7 answers

Make sure to set the code in the resx editor to Public, then you can simply use:

 <TextBlock Text="{x:Static Messages.WarningUserMessage}" /> 
+59
May 27 '10 at 8:01
source share

It is much easier to do so. Add xmlns to the XAML file and use resources directly.

 xmlns:resx="clr-namespace:wpfapplicationname.Properties" Title="{x:Static resx:Resources.name}" 
+51
Sep 16 '11 at 2:45
source share

I understand that my answer was a bit late, but I thought it was worth sharing:

To the user, the line stored in the * .resx file without the Static keyword:

  • In the App.Xaml file, add a namespace for the xmlns:resource="clr-namespace:YourProject.Properties"
  • In ApplicationResources (app.xaml file) Add a resource for your * .resx file

    <Application.Resources> <resource:ResourceFileName x:Key="ApplicationStringResources" /> </Application.Resources>

  • In your XAML file, use the following Binding, let's take an example of a window title

    Title="{Binding TitleString, Source={StaticResource ResourceKey=ApplicationStringResources}}"

    TitleString is the name of the StringProperty in your * .resx file

  • Last but not least, remember to change the access modifier of the file resource in Public.

Enjoy :)

+8
Mar 10 '15 at 5:06
source share

The simplest way, probably, is to directly refer to the elements (they are static properties, internal by default):

 <TextBlock x:Name="txtMessage" Text="{x:Static MyApp.Properties.Resource.TextString}"/> 

If you are working on a localized WPF application, I would recommend taking a look at the CodePlex manual at http://wpflocalization.codeplex.com/ , and if you are creating a composite application (using PRISM or MEF), I have a good message for Perform WPF localization using standard bindings .

+5
May 27 '10 at 8:09 a.m.
source share

After a day exploring this comment. Xaml localization: Using .resx resources in Xaml without x: static I found a simple solution to provide multilingual support (built-in resources or assembly reference) * .resx files. Starting with Framework 4, there is a base DynamicObject class for specifying dynamic runtime behavior in the System.Dynamic namespace.

I got the following ResourceLoader from System.Dynamic.DynamicObject - class:

 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 ----------------------------------------------------------- /// <summary> /// Gets or sets the assembly. /// </summary> public string Assembly { get { return assembly; } set { assembly = value; theAssembly = System.Reflection.Assembly.Load(assembly); _resourceMan = null; } } /// <summary> /// Gets or sets the resources suffix. /// </summary> public string ResourcesSuffix { get { return resourcesSuffix; } set { resourcesSuffix = value; _resourceMan = null; } } /// <summary> /// Get, set culture /// </summary> public CultureInfo CurrentCulture { get { this.culture = this.culture ?? CultureInfo.InvariantCulture; return this.culture; } set { this.culture = value; } } /// <summary> /// Creates new instace of <see cref="System.Resources.ResourceManager"/> at initialisation or change of <see cref="ResourceFileAccessSample.ResourceBinding.ResourceLoader.Assembly"/>. /// </summary> 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); } /// <summary> /// Provides the implementation for operations that get member values. Classes derived from the <see cref="T:System.Dynamic.DynamicObject"/> class can override this method to specify dynamic behavior for operations such as getting a value for a property. /// </summary> /// <param name="binder">Provides information about the object that called the dynamic operation. The binder.Name property provides the name of the member on which the dynamic operation is performed. For example, for the Console.WriteLine(sampleObject.SampleProperty) statement, where sampleObject is an instance of the class derived from the <see cref="T:System.Dynamic.DynamicObject"/> class, binder.Name returns "SampleProperty". The binder.IgnoreCase property specifies whether the member name is case-sensitive.</param> /// <param name="result">The result of the get operation. For example, if the method is called for a property, you can assign the property value to <paramref name="result"/>.</param> /// <returns> /// true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a run-time exception is thrown.) /// </returns> 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; } /// <summary> /// Switch set culture /// </summary> public void SwitchCulture(CultureInfo NewCulture) { this.culture = NewCulture; } #endregion // Methods #region Constructors --------------------------------------------------------- /// <summary> /// Initializes a new instance of the <see cref="ResourceLoader"/> class. /// </summary> public ResourceLoader() : this(CultureInfo.InvariantCulture, DefaultResourcesSuffix) { } /// <summary> /// Initializes a new instance of the <see cref="ResourceLoader"/> class. /// </summary> public ResourceLoader(CultureInfo InitCulture, string ResourceSuffix) { _defaultAssemblyName = GetType().Assembly.GetName().Name; _defaultAssembly = GetType().Assembly; this.culture = InitCulture; this.resourcesSuffix = ResourceSuffix; } #endregion // Constructors } 

You can create an instance inside xaml as follows:

 <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:

  /// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial class Window1 : Window { private ResourceLoader res; public Window1() { InitializeComponent(); // load it from WPF Resources this.res = (ResourceLoader)this.FindResource("resource"); // or create an instance //this.res = new ResourceLoader(CultureInfo.InvariantCulture, "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; } } 

Now you can bind strings and images (images will be converted to the WPF BitmapSource compiler:

  <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> 
+1
Jan 23 '15 at 9:29
source share

Hide another text block and bind its text In this text block, you will have a resource from .cs

0
Sep 22 '15 at 11:22
source share

The easiest way that you can determine the width of a text field is also according to the length of the text in each language.

Xaml Code

 <TextBlock x:Uid="Greeting" Text="" /> 

Resource File: -

Title - Value

Greeting.Text - Hello

Greeting.width - 20

View the resource file: - Click View

0
Aug 22 '17 at 11:14
source share



All Articles