,

Dynamic Localizability—Updating the UI When the Culture Changes

The Windows Phone XAML data-binding infrastructure relies on change notification to signal that a property in a data-binding expression should be reevaluated. Generated resource designer classes do not provide change notification. Simply exposing the generated resource class for data-binding does not allow the UI to be updated when a culture change occurs at runtime. Therefore, some other means is required to update the UI when the app’s culture changes. This is the purpose of the custom BindableChangeNotifier class, shown in Listing 19.2. By using the BindableChangeNotifier class to wrap the resource’s designer class, change notification can be surreptitiously added for all properties.


Note

If the desired CurrentCulture of the thread is set to the appropriate value before the UI is shown, refreshing the data-bindings is not required. If, however, the user is provided with the ability to change the language, after the UI is loaded, the BindableChangeNotifier or some other mechanism is necessary to refresh the data-bindings within the active view.


The ResourceBase class implements System.ComponentModel.INotifyPropertyChanged to signal to the data-binding infrastructure that properties should be reread (see Listing 19.2).

LISTING 19.2. BindableChangeNotifier Class


public sealed class BindableChangeNotifier<T> : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    static T instance;

    public T Instance
    {
        get
        {
            return instance;
        }
    }

    public BindableChangeNotifier(T observableObject)
    {
        instance = observableObject;
    }

    public void RaisePropertyChanged()
    {
        var temp = PropertyChanged;
        if (temp != null)
        {
            temp(this, new PropertyChangedEventArgs(string.Empty));
        }
    }
}


The BindableChangeNotifier class contains a public method called RaisePropertyChanged. By using string.Empty as the property name, it signals to the data-binding infrastructure that all source properties within the resources object should be reread. Thus, it is not necessary to raise the PropertyChanged event for each resource property.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.74.154