Exemple #1
0
        static string GetXamlForType(Type type, object instance, out bool useDesignProperties)
        {
            useDesignProperties = false;
            //the Previewer might want to provide it's own xaml for this... let them do that
            //the check at the end is preferred (using ResourceLoader). keep this until all the previewers are updated

            string xaml;

#pragma warning disable 0618
            if (ResourceLoader.ResourceProvider2 == null && (xaml = Internals.XamlLoader.XamlFileProvider?.Invoke(type)) != null)
            {
                return(xaml);
            }
#pragma warning restore 0618

            var assembly   = type.GetTypeInfo().Assembly;
            var resourceId = XamlResourceIdAttribute.GetResourceIdForType(type);

            var rlr = ResourceLoader.ResourceProvider2?.Invoke(new ResourceLoader.ResourceLoadingQuery {
                AssemblyName = assembly.GetName(),
                ResourcePath = XamlResourceIdAttribute.GetPathForType(type),
                Instance     = instance,
            });
            var alternateXaml = rlr?.ResourceContent;

            if (alternateXaml != null)
            {
                useDesignProperties = rlr.UseDesignProperties;
                return(alternateXaml);
            }

            if (resourceId == null)
            {
                return(LegacyGetXamlForType(type));
            }

            using (var stream = assembly.GetManifestResourceStream(resourceId)) {
                if (stream != null)
                {
                    using (var reader = new StreamReader(stream))
                        xaml = reader.ReadToEnd();
                }
                else
                {
                    xaml = null;
                }
            }

            return(xaml);
        }
        object IValueProvider.ProvideValue(IServiceProvider serviceProvider)
        {
            IXmlLineInfo lineInfo;

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

            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 resString      = DependencyService.Get <IResourcesLoader>().GetResource(resourcePath, rootObjectType.GetTypeInfo().Assembly, lineInfo);
                return(StyleSheet.FromString(resString));
            }

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

            lineInfo = (serviceProvider.GetService(typeof(IXmlLineInfoProvider)) as IXmlLineInfoProvider)?.XmlLineInfo;
            throw new XamlParseException($"StyleSheet require either a Source or a content", lineInfo);
        }
Exemple #3
0
        static string GetXamlForType(Type type)
        {
            //the Previewer might want to provide it's own xaml for this... let them do that
            //the check at the end is preferred (using ResourceLoader). keep this until all the previewers are updated

            string xaml;

#pragma warning disable 0618
            if (ResourceLoader.ResourceProvider == null && (xaml = Internals.XamlLoader.XamlFileProvider?.Invoke(type)) != null)
            {
                return(xaml);
            }
#pragma warning restore 0618


            var assembly   = type.GetTypeInfo().Assembly;
            var resourceId = XamlResourceIdAttribute.GetResourceIdForType(type);

            if (resourceId == null)
            {
                return(LegacyGetXamlForType(type));
            }

            using (var stream = assembly.GetManifestResourceStream(resourceId)) {
                if (stream != null)
                {
                    using (var reader = new StreamReader(stream))
                        xaml = reader.ReadToEnd();
                }
                else
                {
                    xaml = null;
                }
            }

            var alternateXaml = ResourceLoader.ResourceProvider?.Invoke(XamlResourceIdAttribute.GetPathForType(type));
            return(alternateXaml ?? xaml);
        }