0
When to use CultureInfo

When to use CultureInfo

One thing that developers constantly do, is forget to specify culture (CultureInfo) within their applications.
This is one of the more important classes in the framework that is often overlooked.
There are a few different types of culture to set :

 

  • InstalledUICulture
  • CurrentUICulture
  • CurrentCulture
  • InvariantCulture


InstalledUICulture

InstalledUICulture represents the culture installed with the operating system. For Window products that offer a Multilingual User Interface, it will return English (en-US). This is not commonly used.

CurrentUICulture

CurrentUICulture is what should be used to display correct text, colors and images. For example, in an ASP.NET web application where you want to serve localized content; this would be the way to go.

CurrentCulture

CurrentCulture is an important one that should be always be used for formatting before displaying them to a user. For example, if the user’s Culture is “(fr-FR”) the days would be returned as “dimanche”, “lundi”, “mardi”, etc…

 

var days = CultureInfo.CurrentCulture.DateTimeFormat.DayNames;

 

Keep in mind that this property is read from the executing thread and can also be overridden. Another reason to use this is for parsing Locale specific input. This allows users to input data in their regional format.

InvariantCulture

InvariantCulture is on the opposite side of CurrentCulture. When handling internal data in the application or transferring across network, or storing data; this is the way to go. It should always be used. It specifically allows you to parse input in a reliable manner, no matter what the current culture happens to be. This is not used to parse user input.

Keep these properties in mind when working on your next project. If you ever happen to distribute your software to another country, these could save you from tracking down bugs that you can’t reproduce locally.

0
Mapping WPF HotKeys to Actions

While sitting in the office the other day, we had the task of mapping wpf hotkeys to actions for our real time data application. Normally, in a WPF MVVM application, you could do the following in Xaml to assign a hotkey to a command:

<Window.InputBindings>
  <KeyBinding Command="Command2" Key="F2"/>
  <KeyBinding Command="Command1" Key="F1"/>
</Window.InputBindings>

However, to meet our specific requirements, we need to allow a set of hot keys to be loaded dynamically at runtime. For this, we are using MEF and an exported class that gets imported at runtime by the shell.

Here, we are using a dictionary to map each of our keys to an action.

/// <summary>
    /// This class is used to map hot keys to Actions.
    /// </summary>
    [Export(typeof(IHotKeyMapper))]
    public class HotKeyMapper :IHotKeyMapper
    {
        public Dictionary<Key, Action> HotKeyDictionary { get; private set; }

        public HotKeyMapper()
        {
            InitializeDictionary();
        }

        private void InitializeDictionary()
        {
            HotKeyDictionary = new Dictionary<Key, Action>
                                   {
                                       {Key.J,ShowMenuStubMethod },
                                       {Key.D,ShowMenuStubMethod },
                                       {Key.L,ShowMenuStubMethod },
                                       {Key.Y,ShowMenuStubMethod },
                                       {Key.F,ShowMenuStubMethod },
                                       {Key.C,ShowMenuStubMethod },
                                       {Key.T,ShowMenuStubMethod },
                                       {Key.H,ShowMenuStubMethod },
                                       {Key.V,ShowMenuStubMethod },
                                       {Key.O,ShowMenuStubMethod },
                                       {Key.K,ShowMenuStubMethod },
                                       {Key.R,ShowMenuStubMethod },
                                       {Key.E,ShowMenuStubMethod },
                                       {Key.Escape,ShowMenuStubMethod },
                                       {Key.F1,ShowMenuStubMethod },
                                       {Key.F2,ShowMenuStubMethod },
                                       {Key.Tab,ShowMenuStubMethod }
                                   };
        }

        public void ShowMenuStubMethod()
        {
            Dialog.Show("Menu Opened!");
        }
        
    }

Here, we subscribe to the imported HotKeyMapper and Invoke methods as the events are raised.

       

 void Shell_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (HotKeyMapper.HotKeyDictionary.ContainsKey(e.Key))
            {
                HotKeyMapper.HotKeyDictionary[e.Key].Invoke();
            }
        }

There may be more elegant ways to handle this, but this seemed to be a pretty neat idea.

1