public string GetResource(string resourcePath, Assembly assembly, IXmlLineInfo lineInfo) { var alternateResource = ResourceLoader.ResourceProvider?.Invoke(assembly.GetName(), 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()); } }
public string GetResource(string resourcePath, Assembly assembly, object target, IXmlLineInfo lineInfo) { var resourceLoadingResponse = ResourceLoader.ResourceProvider?.Invoke(new 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()); } }
public static void Load(object view, Type callingType) { try { string xaml = ""; var assembly = callingType.GetTypeInfo().Assembly; var resourceId = XamlResourceIdAttribute.GetResourceIdForType(callingType); if (resourceId == null) { xaml = LegacyGetXamlForType(callingType); } else { using (var stream = assembly.GetManifestResourceStream(resourceId)) { if (stream != null) { using (var reader = new StreamReader(stream)) xaml = reader.ReadToEnd(); } else { xaml = null; } } } if (string.IsNullOrEmpty(xaml)) { xaml = GetXamlForType(callingType); if (string.IsNullOrEmpty(xaml)) { throw new XamlParseException(string.Format("Can't get xaml from type {0}", callingType), new XmlLineInfo()); } } Load(view, xaml); } catch (XamlParseException e) { Tizen.Log.Fatal("NUI", "XamlParseException e.Message: " + e.Message); Console.WriteLine("\n[FATAL] XamlParseException e.Message: {0}\n", e.Message); } }
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); }
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(assembly.GetName(), XamlResourceIdAttribute.GetPathForType(type)); return(alternateXaml ?? xaml); }
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; string resourceName = type.Name + ".xaml"; string resource = Tizen.Applications.Application.Current.DirectoryInfo.Resource; Tizen.Log.Fatal("NUI", "the resource path: " + resource); int windowWidth = NUIApplication.GetDefaultWindow().Size.Width; int windowHeight = NUIApplication.GetDefaultWindow().Size.Height; string likelyResourcePath = resource + "layout/" + windowWidth.ToString() + "x" + windowHeight.ToString() + "/" + resourceName; Tizen.Log.Fatal("NUI", "the resource path: " + likelyResourcePath); if (!File.Exists(likelyResourcePath)) { likelyResourcePath = resource + "layout/" + resourceName; } //Find the xaml file in the layout folder if (File.Exists(likelyResourcePath)) { StreamReader reader = new StreamReader(likelyResourcePath); xaml = reader.ReadToEnd(); reader.Close(); reader.Dispose(); Tizen.Log.Fatal("NUI", "File is exist!, try with xaml: " + xaml); var pattern = String.Format("x:Class *= *\"{0}\"", type.FullName); var regex = new Regex(pattern, RegexOptions.ECMAScript); if (regex.IsMatch(xaml) || xaml.Contains(String.Format("x:Class=\"{0}\"", type.FullName))) { return(xaml); } else { throw new XamlParseException(string.Format("Can't find type {0}", type.FullName), new XmlLineInfo()); } } else { Assembly assembly = type.Assembly; var resourceId = XamlResourceIdAttribute.GetResourceIdForType(type); if (null == resourceId) { throw new XamlParseException(string.Format("Can't find type {0} in embedded resource", type.FullName), new XmlLineInfo()); } else { Stream stream = assembly.GetManifestResourceStream(resourceId); if (null != stream) { Byte[] buffer = new byte[stream.Length]; stream.Read(buffer, 0, (int)stream.Length); string ret = System.Text.Encoding.Default.GetString(buffer); return(ret); } else { throw new XamlParseException(string.Format("Can't get xaml stream {0} in embedded resource", type.FullName), new XmlLineInfo()); } } } }