Binding resource with Xaml x:Uid some times not working

746 Views Asked by At

I have an UWP app with two languages. For some labels I've used: Loader.GetString("LableName") in code-behind and for the others have used: x:Uid in XAML. Users can change the CurrentCulture using a button like this:

 CultureInfo.CurrentCulture = new CultureInfo("fa-IR");

For the first time totally work correct, But for the next times, after the button is clicked, those labels that have bound in the code-behind will work properly, But those that have bound in the XAML with X:Uid, still display previous language resource strings.

Can someone help me?

2

There are 2 best solutions below

0
On

You can use the button property: double click on the button, after you have to set the value of the label in the method "button_click".

0
On

In WPF we use something like this to apply culture on window elements (reference), use it in your UWP app, i hope it help:

public MyWindow()
{    
    this.Language = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag);
    InitializeComponent();
} 

You can also set the language globally for your entire application in the App class:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    // Set application startup culture based on config settings
    string culture = ConfigurationManager.AppSettings["Culture"];
    SetCulture(culture);  
    FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement),
    new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));            
}