Exemple #1
0
        public string GetResource(string resourcePath, Assembly assembly, IXmlLineInfo lineInfo)
        {
            var alternateResource = Xamarin.Forms.Internals.ResourceLoader.ResourceProvider?.Invoke(resourcePath);

            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());
            }
        }
Exemple #2
0
        public string GetResource(string resourcePath, Assembly assembly, object target, IXmlLineInfo lineInfo)
        {
            var resourceLoadingResponse = Forms.Internals.ResourceLoader.ResourceProvider2?.Invoke(new Forms.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());
            }
        }
Exemple #3
0
        public T CreateFromResource <T>(string resourcePath, Assembly assembly, IXmlLineInfo lineInfo) where T : new()
        {
            var resourceLoadingResponse = Forms.Internals.ResourceLoader.ResourceProvider2?.Invoke(new Forms.Internals.ResourceLoader.ResourceLoadingQuery {
                AssemblyName = assembly.GetName(),
                ResourcePath = resourcePath
            });

            var alternateResource = resourceLoadingResponse?.ResourceContent;

            if (alternateResource != null)
            {
                var rd = new T();
                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)) {
                    var rd = new T();
                    rd.LoadFromXaml(reader.ReadToEnd());
                    return(rd);
                }
            }
        }
Exemple #4
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);
        }
Exemple #5
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);

            var alternateXaml = ResourceLoader.ResourceProvider?.Invoke(assembly.GetName(), XamlResourceIdAttribute.GetPathForType(type));
            if (alternateXaml != null)
            {
                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);
        }