Пример #1
0
        // 广度优先搜索XML节点树创建相应的控件树,过滤非控件节点
        protected ControlUI parse(ref MarkupNode parentNode, ref ControlUI parentControl, ref PaintManagerUI manager)
        {
            if (parentNode == null)
            {
                throw new Exception("对象未赋值");
            }
            ControlUI ctlParent = null;

            {
                //parentControl.setManager(manager, null);

                Queue <MarkupNode> queueNode    = new Queue <MarkupNode>();
                Queue <ControlUI>  queueControl = new Queue <ControlUI>();

                queueNode.Enqueue(parentNode);
                queueControl.Enqueue(parentControl);

                while (queueNode.Count > 0)
                {
                    MarkupNode        curNode          = queueNode.Dequeue();
                    ControlUI         curParentControl = queueControl.Dequeue();
                    List <MarkupNode> listNode         = curNode.getChildList();

                    // 访问根节点
                    if (listNode != null && listNode.Count > 0)
                    {
                        // 子节点入队
                        foreach (var node in listNode)
                        {
                            // 过滤非控件节点
                            if (node.getName() == "Window" ||
                                node.getName() == "Image" ||
                                node.getName() == "Font" ||
                                node.getName() == "Default")
                            {
                                continue;
                            }

                            ControlUI newControl = null;
                            {
                                queueNode.Enqueue(node);

                                // 创建控件,加入控件树后入队
                                newControl = getControl(node.getName());

                                if (newControl != null && newControl is ControlUI)
                                {
                                    queueControl.Enqueue(newControl);

                                    newControl.setManager(manager, curParentControl);
                                    if (curParentControl != null)
                                    {
                                        IContainerUI container = (IContainerUI)curParentControl.getInterface("IContainer");
                                        container.add(newControl);
                                    }
                                    else
                                    {
                                        if (ctlParent != null)
                                        {
                                            throw new Exception("不能有两个根容器");
                                        }
                                        ctlParent = newControl;
                                    }
                                }
                                else if (mCallback != null)
                                {
                                    newControl = mCallback.createControl(node.getName(), manager);
                                    if (newControl == null)
                                    {
                                        throw new Exception("未知控件类型");
                                    }
                                    queueControl.Enqueue(newControl);

                                    newControl.setManager(manager, curParentControl);
                                    if (curParentControl != null)
                                    {
                                        IContainerUI container = (IContainerUI)curParentControl.getInterface("IContainer");
                                        container.add(newControl);
                                    }
                                    else
                                    {
                                        if (ctlParent != null)
                                        {
                                            throw new Exception("不能有两个根容器");
                                        }
                                        ctlParent = newControl;
                                    }
                                }
                                else
                                {
                                    throw new Exception("未知控件类型");
                                }
                            }

                            {
                                // 设置属性
                                if (manager != null)
                                {
                                    //newControl.setManager(manager, curParentControl);
                                    string defaultAttributes = manager.getDefaultAttributeList(node.getName());
                                    if (defaultAttributes != "")
                                    {
                                        newControl.applyAttributeList(defaultAttributes);
                                    }
                                }

                                List <XMLAttribute> listAttr = node.getAttributeList();
                                foreach (var attr in listAttr)
                                {
                                    newControl.addAttribute(attr.getName(), attr.getValue());
                                    newControl.setAttribute(attr.getName(), attr.getValue());
                                }
                            }
                        }
                    }
                }
            }

            return(ctlParent);
        }