Exemple #1
0
        }         // func ReadConditionAsync

        /// <summary></summary>
        /// <param name="xml"></param>
        /// <param name="priority"></param>
        /// <returns></returns>
        public async Task <int> AppendTemplateAsync(XmlReader xml, int priority)
        {
            // get base attributes
            priority = xml.GetAttribute("priority", priority + 1);

            // read start element
            await xml.ReadAsync();

            // read optional condition
            var condition =
                await xml.ReadOptionalStartElementAsync(StuffUI.xnCondition)
                                        ? await ReadConditionAsync(xml)
                                        : null;

            // read template
            var template = await PpsXamlParser.LoadAsync <DataTemplate>(xml.ReadElementAsSubTree(), new PpsXamlReaderSettings { ServiceProvider = Environment, Code = templateCode });

            var templateItem = new TemplateItem(priority, condition, template);

            // insert the item in order of the priority
            var index = templates.BinarySearch(templateItem);

            if (index < 0)
            {
                templates.Insert(~index, templateItem);
            }
            else
            {
                templates.Insert(index, templateItem);
            }

            return(priority);
        }         // proc AppendTemplate
        private PpsGenericWpfControl UpdateControl(object args)
        {
            PpsGenericWpfControl returnValue;

            if (args is PpsGenericWpfControl paneControl)             // force a framework element
            {
                returnValue = paneControl;
            }
            else if (args is LuaTable t)             // should be properties for the PpsGenericWpfControl
            {
                var creator = LuaWpfCreator.CreateFactory(ShellWpf.LuaUI, typeof(PpsGenericWpfControl));
                creator.SetTableMembers(t);
                using (var xamlReader = new PpsXamlReader(creator.CreateReader(this), new PpsXamlReaderSettings()
                {
                    Code = this, CloseInput = true, BaseUri = Request.BaseAddress, ServiceProvider = this
                }))
                    returnValue = PpsXamlParser.LoadAsync <PpsGenericWpfControl>(xamlReader).AwaitTask();
            }
            else
            {
                throw new ArgumentException(nameof(args));
            }

            Control = returnValue;
            OnControlCreated();

            return(Control);
        }         // proc UpdateControl
        /// <summary></summary>
        /// <param name="parentPane"></param>
        /// <param name="paneData"></param>
        /// <param name="fullUri"></param>
        public PpsGenericWpfChildPane(PpsGenericWpfWindowPane parentPane, object paneData, Uri fullUri)
            : base(parentPane)
        {
            if (parentPane == null)
            {
                throw new ArgumentNullException("parentPane");
            }

            // create a new request object for this child pane
            Request = parentPane.Shell.CreateHttp(new Uri(fullUri, "."));

            // create the control
            if (paneData is XDocument xamlCode)
            {
                Control = PpsXamlParser.LoadAsync <FrameworkElement>(PpsXmlPosition.CreateLinePositionReader(xamlCode.CreateReader()), new PpsXamlReaderSettings()
                {
                    Code = this, CloseInput = true, ServiceProvider = (IServiceProvider)Parent
                }).AwaitTask();
            }
            else if (paneData is LuaChunk luaCode)             // run the chunk on the current table
            {
                luaCode.Run(this, this);
            }
            else
            {
                throw new ArgumentException();                 // todo:
            }
            Control.DataContext = this;
            CallMemberDirect("OnControlCreated", Array.Empty <object>(), rawGet: true, ignoreNilFunction: true);
        }         // ctor
        /// <summary>Load the content of the panel</summary>
        /// <param name="arguments"></param>
        /// <returns></returns>
        protected virtual async Task LoadInternAsync(LuaTable arguments)
        {
            // save the arguments
            this.Arguments = arguments;

            // prepare the base
            var paneUri = GetPaneUri(arguments, true);

            Request = Shell.CreateHttp(new Uri(paneUri, "."));

            // Load the xaml file and code
            var paneData = await this.LoadPaneDataAsync(arguments, paneUri);

            // Create the Wpf-Control
            if (paneData is XDocument xamlCode)
            {
                //PpsXamlParser.DebugTransform = true;
                Control = await PpsXamlParser.LoadAsync <PpsGenericWpfControl>(PpsXmlPosition.CreateLinePositionReader(xamlCode.CreateReader()),
                                                                               new PpsXamlReaderSettings()
                {
                    BaseUri         = paneUri,
                    Code            = this,
                    ServiceProvider = this,
                    ParserServices  = GetRootParserServices()
                }
                                                                               );

                //PpsXamlParser.DebugTransform = false;
                Control.Resources[PpsShellWpf.CurrentWindowPaneKey] = this;
                OnControlCreated();
            }
            else if (paneData is LuaChunk luaCode)             // run the code to initalize control, setControl should be called.
            {
                Shell.RunScript(luaCode, this, true, this);
            }

            if (Control == null)
            {
                throw new ArgumentException("No control created.");
            }

            // init bindings
            Control.DataContext = this;

            DependencyPropertyDescriptor.FromProperty(PpsGenericWpfControl.TitleProperty, typeof(PpsGenericWpfControl)).AddValueChanged(Control, ControlTitleChanged);
            DependencyPropertyDescriptor.FromProperty(PpsGenericWpfControl.SubTitleProperty, typeof(PpsGenericWpfControl)).AddValueChanged(Control, ControlSubTitleChanged);
            DependencyPropertyDescriptor.FromProperty(PpsGenericWpfControl.HasSideBarProperty, typeof(PpsGenericWpfControl)).AddValueChanged(Control, ControlHasSideBarChanged);

            // notify changes on control
            OnPropertyChanged(nameof(Control));
            OnPropertyChanged(nameof(Title));
            OnPropertyChanged(nameof(SubTitle));
            OnPropertyChanged(nameof(IPpsWindowPane.HasSideBar));
            OnPropertyChanged(nameof(Commands));
        }         // proc LoadInternAsync
Exemple #5
0
        /// <summary>Update a resource from a xml-source.</summary>
        /// <param name="xamlSource"></param>
        /// <returns></returns>
        protected async Task <object> UpdateResourceAsync(XmlReader xamlSource)
        {
            var    resourceKey = (object)null;
            object resource;

            try
            {
                // create the resource
                using (var elementReader = await xamlSource.ReadElementAsSubTreeAsync(XmlNamespaceScope.ExcludeXml))
                {
                    var startObjectCounter = 0;

                    resource = await PpsXamlParser.LoadAsync <object>(elementReader,
                                                                      new PpsXamlReaderSettings()
                    {
                        FilterNode = (reader) =>
                        {
                            switch (reader.NodeType)
                            {
                            case XamlNodeType.GetObject:
                            case XamlNodeType.StartObject:
                                startObjectCounter++;
                                goto default;

                            case XamlNodeType.EndObject:
                                startObjectCounter--;
                                goto default;

                            case XamlNodeType.StartMember:
                                if (startObjectCounter == 1 && reader.Member == XamlLanguage.Key && resourceKey == null)
                                {
                                    resourceKey = reader.ReadMemberValue();
                                    return(reader.Read());
                                }
                                goto default;

                            default:
                                return(true);
                            }
                        },
                        CloseInput      = false,
                        Code            = this,
                        ServiceProvider = this
                    }
                                                                      );
                }

                if (resource == null)
                {
                    throw Procs.CreateXmlException(xamlSource, "Resource load failed.");
                }

                // check the key
                if (resourceKey == null)
                {
                    var style = resource as Style;
                    if (style == null)
                    {
                        throw Procs.CreateXmlException(xamlSource, "x:Key is missing on resource.");
                    }

                    resourceKey = style.TargetType;
                }
            }
            catch (XmlException)             // no recovery, xml is invalid
            {
                throw;
            }
            catch (Exception e)             // xaml parser error
            {
                // check resource key
                if (resourceKey == null ||              // no resource key to check, do not load
                    defaultResources[resourceKey] == null)                        // no resource, might be imported
                {
                    throw;
                }
                else
                {
                    var message = $"Could not load resource '{resourceKey}'.";
                    if (e is XamlParseException)
                    {
                        AppendException(e, message);
                    }
                    else
                    {
                        AppendException(Procs.CreateXmlException(xamlSource, message, e));
                    }

                    return(null);
                }
            }

            // update resource
            //Debug.Print("UpdateResouce: ({1}){0}", resourceKey, resourceKey.GetType().Name);
            defaultResources[resourceKey] = resource;
            return(resourceKey);
        }         // func UpdateResource