Why can't I use CultureInfo.CurrentCulture in XAML during development?

I have the following XAML:

<TextBlock Text="{Binding Source={x:Static s:DateTime.Now}, StringFormat=Date: {0:dddd, MMMM dd}}"/> 

s:DateTime.Now with xmlns:s="clr-namespace:System;assembly=mscorlib" works great at runtime, as well as in development mode (Visual Studio 2015 Enterprise).

But if I try the same with CultureInfo.CurrentCulture , then this only works at runtime and gives me an error in development mode ( xmlns:c="clr-namespace:System.Globalization;assembly=mscorlib" ):

 <TextBlock Text="{Binding Source={x:Static s:DateTime.Now}, ConverterCulture={x:Static c:CultureInfo.CurrentCulture}, StringFormat=Date: {0:dddd, MMMM dd}}"/> 

I am not looking for a workaround. I am just trying to understand the difference between DateTime.Now and CultureInfo.CurrentCulture and why one of them works and the other does not.

+5
source share
3 answers

I know that you did not ask for a workaround, and I cannot answer your original question.

I still want to post my decision if others, like me, stumble over your question in search of a workaround.

If you install ConverterCulture in the CustomBinding class and use this CustomBinding instead of Binding in xaml, it also works during development.

 public class CultureAwareBinding : System.Windows.Data.Binding { public CultureAwareBinding() { ConverterCulture = CultureInfo.CurrentCulture; } } 

You can use it in your haml like this.

 <TextBlock Text="{CultureAwareBinding Source={x:Static s:DateTime.Now}, StringFormat=Date: {0:dddd, MMMM dd}}"/> 

As an added benefit, this allows you to convert the Converter later in one place. You can also set other properties, such as StringFormat, like this.

0
source

I saw the same behavior, and the best answer is to consider CurrentCulture and CurrentUICulture something that is not used during development if you do not install it. As with many other WPF presentation constructs, sometimes your presentation model or other properties need to take into account design time so that your visual elements appear as expected in your VS designer. For example, the MVVM Galasoft infrastructure includes the IsDesignTime property. You can use this (or whatever your own framework provides) for a special case for a property, if IsDesignTime is true, set it to some value suitable for display. This is convenient, for example, when you want to see how it appears when you install this culture in other languages.

Thus, for your example, assuming that you are really using an architecture of type MVVM, you can bind your TextBlock Text property to your own view-model property and use this property to determine the current DateTime and CurrentCulture.

One note: remember that with WPF on desktop applications, the Window or UserControl ctor window does not execute in the development environment β€” only at run time. It can get confused until you realize it.

0
source

I had the same problems not so long ago, I would very much suggest using this package nugget for localization. It is quite easy to use. https://www.nuget.org/packages/WpfLocalizeExtension/

Example AssemblyName = Application

RessourceFile = MainApplication.resx

Ressource1Key1 = Test

If you are using MVVM, here is a snippet of code.

Xaml

  xmlns:lex="http://wpflocalizeextension.codeplex.com" lex:LocalizeDictionary.DesignCulture="en" lex:ResxLocalizationProvider.DefaultAssembly="AssemblyName" lex:ResxLocalizationProvider.DefaultDictionary="ressourceName" 

Defining content for tags, buttons, etc.

 <Label Content="{lex:Loc AssemblyName:ResourceName:ResourceKey}"> <Label Content="{lex:Loc Application:MainApplication:Test}"> 

In ViewModel, you want to bind your button to the command FrenchUI, EnglishUI

 private void FrenchUI() { LocalizeDictionary.Instance.SetCurrentThreadCulture = true; LocalizeDictionary.Instance.Culture = new CultureInfo(ConfigurationManager.AppSettings["Culture"])//fr-CA; } private void EnglishUI() { LocalizeDictionary.Instance.SetCurrentThreadCulture = true; LocalizeDictionary.Instance.Culture = new CultureInfo(ConfigurationManager.AppSettings["CultureUS"]);//en-US } 
0
source

Source: https://habr.com/ru/post/1241987/


All Articles