Beispiel #1
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