示例#1
0
        public string GetResource(string resourcePath, Assembly assembly, object target, IXmlLineInfo lineInfo)
        {
            var resourceLoadingResponse = Maui.Controls.Internals.ResourceLoader.ResourceProvider2?.Invoke(new Maui.Controls.Internals.ResourceLoader.ResourceLoadingQuery
            {
                AssemblyName = assembly.GetName(),
                ResourcePath = resourcePath,
                Instance     = target
            });

            var alternateResource = resourceLoadingResponse?.ResourceContent;

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

            var resourceId = XamlResourceIdAttribute.GetResourceIdForPath(assembly, resourcePath);

            if (resourceId == null)
            {
                throw new XamlParseException($"Resource '{resourcePath}' not found.", lineInfo);
            }

            using (var stream = assembly.GetManifestResourceStream(resourceId))
            {
                if (stream == null)
                {
                    throw new XamlParseException($"No resource found for '{resourceId}'.", lineInfo);
                }
                using (var reader = new StreamReader(stream))
                    return(reader.ReadToEnd());
            }
        }
示例#2
0
        public T CreateFromResource <T>(string resourcePath, Assembly assembly, IXmlLineInfo lineInfo) where T : new()
        {
            var rd = new T();

            var resourceLoadingResponse = Maui.Controls.Internals.ResourceLoader.ResourceProvider2?.Invoke(new Maui.Controls.Internals.ResourceLoader.ResourceLoadingQuery
            {
                AssemblyName = assembly.GetName(),
                ResourcePath = resourcePath,
                Instance     = rd,
            });

            var alternateResource = resourceLoadingResponse?.ResourceContent;

            if (alternateResource != null)
            {
                XamlLoader.Load(rd, alternateResource, resourceLoadingResponse.UseDesignProperties);
                return(rd);
            }

            var resourceId = XamlResourceIdAttribute.GetResourceIdForPath(assembly, resourcePath);

            if (resourceId == null)
            {
                throw new XamlParseException($"Resource '{resourcePath}' not found.", lineInfo);
            }

            using (var stream = assembly.GetManifestResourceStream(resourceId))
            {
                if (stream == null)
                {
                    throw new XamlParseException($"No resource found for '{resourceId}'.", lineInfo);
                }
                using (var reader = new StreamReader(stream))
                {
                    rd.LoadFromXaml(reader.ReadToEnd(), assembly);
                    return(rd);
                }
            }
        }
示例#3
0
        object IValueProvider.ProvideValue(IServiceProvider serviceProvider)
        {
            IXmlLineInfo lineInfo;

            if (!string.IsNullOrEmpty(Style) && Source != null)
            {
                throw new XamlParseException($"StyleSheet can not have both a Source and a content", serviceProvider);
            }

            if (Source != null)
            {
                lineInfo = (serviceProvider.GetService(typeof(IXmlLineInfoProvider)) as IXmlLineInfoProvider)?.XmlLineInfo;
                if (Source.IsAbsoluteUri)
                {
                    throw new XamlParseException($"Source only accepts Relative URIs", lineInfo);
                }

                var rootObjectType = (serviceProvider.GetService(typeof(IRootObjectProvider)) as IRootObjectProvider)?.RootObject.GetType();
                if (rootObjectType == null)
                {
                    return(null);
                }
                var rootTargetPath = XamlResourceIdAttribute.GetPathForType(rootObjectType);
                var resourcePath   = ResourceDictionary.RDSourceTypeConverter.GetResourcePath(Source, rootTargetPath);
                var assembly       = rootObjectType.Assembly;

                return(StyleSheet.FromResource(resourcePath, assembly, lineInfo));
            }

            if (!string.IsNullOrEmpty(Style))
            {
                using (var reader = new StringReader(Style))
                    return(StyleSheet.FromReader(reader));
            }

            throw new XamlParseException($"StyleSheet require either a Source or a content", serviceProvider);
        }