# Tuesday, April 14, 2009

Utilisation des behaviors pour localiser une application silverlight ou WPF

Il y a quelques temps déjà, j’ai ajouté à slextensions une classe permettant de localiser simplement une application à partir d’attachedproperty. La mode étant aux behaviors, j’ai rajouté une classe permettant cette localisation à la branche SL3 de slextensions. Cette classe possède 4 propriétés :

  • Key : la clé de la ressource provenant de votre ResourceManager
  • PropertyName : Le nom de la propriété à localiser
  • ResourceManagerKey : La clé avec laquelle retrouver le ResourceManager dans les ressources de l’application
  • ConvertXaml : Si la resource contient du Xaml, permet d’instancier ce xaml par le XamlReader.

L’utilisation est très simple voici un exemple de localisation d’un texte

            <TextBlock  >

                <i:Interaction.Behaviors>

                    <SLExtensions_Interactivity:Localize Key="WelcomeMessage" PropertyName="Text" ResourceManagerKey="MyResource" />

                </i:Interaction.Behaviors>

            </TextBlock>

et d’un ContentControl

            <ContentControl Height="121" Width="244" Canvas.Left="25" Canvas.Top="206" >

                <i:Interaction.Behaviors>

                    <SLExtensions_Interactivity:Localize Key="Welcome" PropertyName="Content" ResourceManagerKey="MyResource" ConvertXaml="True"/>

                </i:Interaction.Behaviors>

            </ContentControl>

Le code de la classe :

using System;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Ink;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using Microsoft.Expression.Interactivity;

using System.Resources;

using System.Globalization;

using System.Windows.Markup;

using SLExtensions.Globalization;

 

namespace SLExtensions.Interactivity

{

    public class Localize : Behavior<FrameworkElement>

    {

 

        private string key;

 

        public string Key

        {

            get

            {

                return this.key;

            }

 

            set

            {

                if (this.key != value)

                {

                    this.key = value;

                    Refresh();

                }

            }

        }

 

        private bool convertXaml;

 

        public bool ConvertXaml

        {

            get

            {

                return this.convertXaml;

            }

 

            set

            {

                if (this.convertXaml != value)

                {

                    this.convertXaml = value;

                    Refresh();

                }

            }

        }

 

 

        private DependencyProperty dependencyProperty;

 

        private string propertyName;

 

        public string PropertyName

        {

            get

            {

                return this.propertyName;

            }

 

            set

            {

                if (this.propertyName != value)

                {

                    this.propertyName = value;

                    this.dependencyProperty = null;

                    Refresh();

                }

            }

        }

 

        private string resourceManagerKey;

 

        public string ResourceManagerKey

        {

            get

            {

                return this.resourceManagerKey;

            }

 

            set

            {

                if (this.resourceManagerKey != value)

                {

                    this.resourceManagerKey = value;

                }

            }

        }

 

        private void Refresh()

        {

            if (AssociatedObject == null

                || string.IsNullOrEmpty(propertyName)

                || string.IsNullOrEmpty(Key)

                || string.IsNullOrEmpty(ResourceManagerKey))

                return;

 

            if (dependencyProperty == null)

            {

                Type t = AssociatedObject.GetType();

                var field = t.GetField(propertyName + "Property", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);

                if (field != null)

                {

                    dependencyProperty = field.GetValue(null) as DependencyProperty;

                }

            }

 

            if (dependencyProperty != null

                && Application.Current != null)

            {

                ResourceManager resourceManager = null;

                var obj = Application.Current.Resources[ResourceManagerKey];

                if (obj is ResourceLoader)

                {

                    resourceManager = ((ResourceLoader)obj).GetResourceManager();

                }

                else

                {

                    resourceManager = obj as ResourceManager;

                }

 

                var resourceValue = resourceManager.GetObject(Key, CultureInfo.CurrentCulture);

                if (convertXaml && resourceValue is string)

                {                   

                    resourceValue = XamlReader.Load((string)resourceValue);

                }

                AssociatedObject.SetValue(dependencyProperty, resourceValue);

            }

        }

 

        protected override void OnAttached()

        {

            Refresh();

            base.OnAttached();

        }

 

        protected override void OnDetaching()

        {

            base.OnDetaching();

        }

    }

}

#    Comments [0] |