Exemplo n.º 1
0
        private void TranslateWindow(Window window)
        {
            string      name = window.GetType().Name;
            BamlControl baml = LoadBaml(name);

            TranslateElement(window, baml);
        }
Exemplo n.º 2
0
        private void TranslateElement(FrameworkElement element, BamlControl baml)
        {
            if ((element == null) || (baml == null))
            {
                return;
            }

            // Translate child controls
            IEnumerable children = LogicalTreeHelper.GetChildren(element);

            foreach (object child in children)
            {
                if (child is FrameworkElement)
                {
                    FrameworkElement childElement = (FrameworkElement)child;
                    TranslateElement(childElement, baml.Find(childElement));
                }
            }

            // Translate this control
            if ((TranslateElementEvent == null) || TranslateElementEvent(this, element))
            {
                PropertyInfo[] properties = element.GetType().GetProperties();

                foreach (PropertyInfo property in properties)
                {
                    Object value;

                    if (property.Name == "Content")
                    {
                        value = baml.Value;
                    }
                    else
                    {
                        value = baml.FindProperty(property.Name);
                    }

                    if ((value != null) && property.CanWrite && (value is string))
                    {
                        //property.SetValue(element, ((string)value).ToUpper(), null);
                        property.SetValue(element, value, null);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public BamlControl Find(FrameworkElement element)
        {
            string name = element.Name;

            if (name == "")
            {
                name = element.Uid;
            }

            BamlControl result = Find(name);

            if (result != null)
            {
                return(result);
            }

            // Check if element is in Children
            result = FindMemberName("Children");

            if (result != null)
            {
                result = result.Find(name);
            }

            if (result != null)
            {
                return(result);
            }

            // Check if element is in Items
            result = FindMemberName("Items");

            if (result != null)
            {
                result = result.Find(name);
            }

            return(result);
        }
Exemplo n.º 4
0
 public BamlControl Add(BamlControl value)
 {
     controls.Add(value);
     value.Parent = this;
     return(value);
 }
Exemplo n.º 5
0
        private BamlControl LoadBaml(string name)
        {
            if (assembly == null)
            {
                assembly = Assembly.GetCallingAssembly();
            }

            if (assemblyName == "")
            {
                assemblyName = Language.GetAssemblyName(Assembly.GetCallingAssembly());
            }

            string language = Language.Id;

            if (language == "")
            {
                language = Thread.CurrentThread.CurrentUICulture.ToString();
            }

            string bamlName = name.ToLower() + ".baml";

            // Get the name of g-resource
            string resourceName = assemblyName + ".g";

            if ((language != "") && (language != Language.OriginalId))
            {
                resourceName = resourceName + "." + language;
            }

            resourceName = resourceName + ".resources";

            Stream      stream  = assembly.GetManifestResourceStream(resourceName);
            BamlControl current = null;

            using (var resourceReader = new ResourceReader(stream))
            {
                foreach (DictionaryEntry entry in resourceReader)
                {
                    if (entry.Key.ToString() == bamlName)
                    {
                        Stream bamlStream = entry.Value as Stream;

                        var    bamlReader = new Baml2006Reader(bamlStream);
                        string memberName = "";

                        var baml = new BamlControl();
                        current = null;

                        while (bamlReader.Read())
                        {
                            XamlNodeType nodeType = bamlReader.NodeType;

                            switch (bamlReader.NodeType)
                            {
                            case XamlNodeType.StartObject:
                                if (current == null)
                                {
                                    current = baml;
                                }
                                else
                                {
                                    current = current.Add(new BamlControl());
                                }

                                if (bamlReader.Type != null)
                                {
                                    current.ElementName = bamlReader.Type.ToString();
                                }

                                break;

                            case XamlNodeType.GetObject:
                                current            = current.Add(new BamlControl());
                                current.MemberName = memberName;
                                break;

                            case XamlNodeType.EndObject:
                                if (current != null)
                                {
                                    current = current.Parent;
                                }

                                break;

                            case XamlNodeType.StartMember:
                                memberName = bamlReader.Member.Name;
                                break;

                            case XamlNodeType.EndMember:
                                memberName = "";
                                break;

                            case XamlNodeType.Value:
                                if (memberName == "Name")
                                {
                                    current.Name = bamlReader.Value.ToString();
                                }
                                else if (memberName == "Uid")
                                {
                                    current.Uid = bamlReader.Value.ToString();
                                }
                                else if (memberName == "Content")
                                {
                                    current.Value = bamlReader.Value;
                                }
                                else if (memberName != "")
                                {
                                    current.AddProperty(memberName, bamlReader.Value);
                                }

                                break;
                            }
                        }

                        return(baml);
                    }
                }
            }

            return(null);
        }