Click here to Skip to main content
15,879,081 members
Articles / Desktop Programming / XAML

Silverlight 1.1 Hebrew and Arabic Language Support

Rate me:
Please Sign up or sign in to vote.
4.05/5 (8 votes)
31 Jan 2008Ms-PL7 min read 44K   290   10  
An article presenting Silverlight 1.1 Hebrew and Arabic language support
using System;
using System.Reflection;
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;


namespace JustinAngelNet.Silverlight.Framework
{
    public abstract class ControlExtenderBase : Control
    {
        public ControlExtenderBase()
        {
            this.Loaded += new EventHandler(ControlExtenderBase_Loaded);
        }

        private void ControlExtenderBase_Loaded(object sender, EventArgs e)
        {
            FillInternalListsBasedOnNameProperties();
            RaiseBaseLoaded();
        }

        private void FillInternalListsBasedOnNameProperties()
        {
            IterateOverAllPropertiesAndSearchForPropertiesWithGetterSetterAndFillByNameAttrbiutes();
        }

        private void IterateOverAllPropertiesAndSearchForPropertiesWithGetterSetterAndFillByNameAttrbiutes()
        {
            foreach (
                PropertyInfo curPropertyInfoToCheckIfIsNameAndIfSoBuildListOnIt in
                    this.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.SetProperty |
                                                 BindingFlags.Instance | BindingFlags.NonPublic))
            {
                object[] FillByNameAttributes =
                    curPropertyInfoToCheckIfIsNameAndIfSoBuildListOnIt.GetCustomAttributes(
                        typeof (FillXamlElementssByName), true);

                if (FillByNameAttributes.Length != 0)
                {
                    FillPropertyWithXamlElementsFromName(curPropertyInfoToCheckIfIsNameAndIfSoBuildListOnIt,
                                                         FillByNameAttributes);
                }
            }
        }

        private void FillPropertyWithXamlElementsFromName(
            PropertyInfo curPropertyInfoToCheckIfIsNameAndIfSoBuildListOnIt, object[] FillByNameAttributes)
        {
            object currentPropertyValue = GetCurrentPropertyValue(curPropertyInfoToCheckIfIsNameAndIfSoBuildListOnIt);
            foreach (FillXamlElementssByName fillXamlElementssByName in FillByNameAttributes)
            {
                Type GenericTypeForList =
                    curPropertyInfoToCheckIfIsNameAndIfSoBuildListOnIt.PropertyType.GetGenericArguments()[0];
                string propertyValueContainsCVSNames = GetPropertyNameValues(fillXamlElementssByName);
                if (propertyValueContainsCVSNames != null &&
                    !String.IsNullOrEmpty(propertyValueContainsCVSNames.ToString()))
                    foreach (
                        string curElementNameToFindAndIfExistsToAdd in
                            propertyValueContainsCVSNames.ToString().Replace(" ", "").Split(','))
                    {
                        CheckIfStringIsAValidFrameworkElementToBeAddedToThePropertyListBasedOnType(
                            currentPropertyValue, GenericTypeForList, curElementNameToFindAndIfExistsToAdd);
                    }
            }
        }

        private void CheckIfStringIsAValidFrameworkElementToBeAddedToThePropertyListBasedOnType(
            object currentPropertyValue, Type GenericTypeForList, string curElementNameToFindAndIfExistsToAdd)
        {
            object curElementToFindAndIfExistsToAdd =
                this.FindName(curElementNameToFindAndIfExistsToAdd);
            if (curElementToFindAndIfExistsToAdd != null)
            {
                if (!GenericTypeForList.IsInstanceOfType(curElementToFindAndIfExistsToAdd))
                    throw new ArgumentException(curElementNameToFindAndIfExistsToAdd + " is not of type " +
                                                GenericTypeForList);
                AddToObjectWithAddMethod(currentPropertyValue, curElementToFindAndIfExistsToAdd);
            }
        }

        private object GetCurrentPropertyValue(PropertyInfo curPropertyInfoToCheckIfIsNameAndIfSoBuildListOnIt)
        {
            object currentPropertyValue;
            try
            {
                currentPropertyValue =
                    curPropertyInfoToCheckIfIsNameAndIfSoBuildListOnIt.GetValue(this, new object[] {});
            }
            catch (Exception ex)
            {
                throw new SilverlightToolboxException(
                    string.Format("get or set for property {0}.{1} is not accessiable",
                                  curPropertyInfoToCheckIfIsNameAndIfSoBuildListOnIt.ReflectedType.Name,
                                  curPropertyInfoToCheckIfIsNameAndIfSoBuildListOnIt.Name), ex);
            }
            if (currentPropertyValue == null)
                throw new SilverlightToolboxException(
                    string.Format("Please set an initial value for {0}",
                                  curPropertyInfoToCheckIfIsNameAndIfSoBuildListOnIt.Name));
            return currentPropertyValue;
        }

        private string GetPropertyNameValues(FillXamlElementssByName fillXamlElementssByName)
        {
            PropertyInfo propertyWithXamlElementsName =
                this.GetType().GetProperty(fillXamlElementssByName.NameOfPropertyWithNamesOfXAMLElements);
            object namesPropertyValue = propertyWithXamlElementsName.GetValue(this, new object[] {});
            if (namesPropertyValue != null)
                return namesPropertyValue.ToString();
            else
                return string.Empty;
        }


        public event EventHandler BaseLoaded;

        public void RaiseBaseLoaded()
        {
            if (BaseLoaded != null)
                BaseLoaded(this, new EventArgs());
        }


        private void AddToObjectWithAddMethod(object ObjectWithAddMethod, object itemToAdd)
        {
            MethodInfo addMethodInfo = ObjectWithAddMethod.GetType().GetMethod("Add");
            if (addMethodInfo != null)
            {
                addMethodInfo.Invoke(ObjectWithAddMethod, new object[] {itemToAdd});
            }
        }


        protected Canvas RootCanvas
        {
            get { return GetRootCanvas(this); }
        }

        public Canvas ParentCanvas
        {
            get
            {
                if (this.Parent == null)
                    return null;
                return this.Parent as Canvas;
            }
        }

        private static Canvas GetRootCanvas(FrameworkElement elementToCheckIfHasNoParentThenIsRoot)
        {
            if (elementToCheckIfHasNoParentThenIsRoot.Parent == null)
                return (Canvas) elementToCheckIfHasNoParentThenIsRoot;
            else
                return GetRootCanvas((FrameworkElement) elementToCheckIfHasNoParentThenIsRoot.Parent);
        }

        public bool Disabled
        {
            get;
            set;
        }

        protected bool Enabled
        {
            get { return !Disabled; }
            set { Disabled = !value; }
        }


        private new Point RenderTransformOrigin
        {
            get;
            set;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
JustinAngel.Net, Senior .Net consultant
Israel Israel
Justin-Josef Angel is a C# Microsoft Most Valuable professional, a Senior .Net consultant in Israel with 4 years of .Net experience and has 8 years of Web experience.

Justin's been working this past year on two Enterprise sized Silverlight projects with his customers. During that time he's gained a real-insight into Silverlight's inner workings and how to integrate Silverlight into the real world of software development. Additionally, During that time he's developed a few well-known projects like the "Silverlight 1.0 Javascript Intellisense", "Silverlight 1.1 Hebrew & Arabic Languages support" and a few others you might know.

Justin is also a seasoned presenter with an impressive track-record of talking in front of thousands of people in Israel.

Justin owns the first .Net blog written in Hebrew - http://www.JustinAngel.Net .
And he also owns an additional blog with mixed Hebrew & English content - http://blogs.Microsoft.co.il/blogs/JustinAngel.

A full list of his articles (all 100+ of them) can be found at: http://www.JustinAngel.Net/#index




Comments and Discussions