diff --git a/Perspex.Xaml.Desktop/InflatableResourceTranslator.cs b/Perspex.Xaml.Desktop/InflatableResourceTranslator.cs new file mode 100644 index 0000000000..0c4e8e228f --- /dev/null +++ b/Perspex.Xaml.Desktop/InflatableResourceTranslator.cs @@ -0,0 +1,78 @@ +namespace Perspex.Xaml.Desktop +{ + using System; + using System.Globalization; + using System.IO; + using System.Linq; + using System.Reflection; + using System.Resources; + using OmniXaml.AppServices; + + public class InflatableResourceTranslator : IInflatableTranslator + { + public Stream GetStream(Type type) + { + var uri = GetUriFor(type); + var assembly = Assembly.GetEntryAssembly(); + var resourceName = assembly.GetName().Name + ".g"; + var manager = new ResourceManager(resourceName, assembly); + + using (ResourceSet resourceSet = manager.GetResourceSet(CultureInfo.CurrentCulture, true, true)) + { + Stream s = (Stream)resourceSet.GetObject(uri.ToString(), true); + + if (s == null) + { + throw new IOException( + "The requested resource could not be found: " + + uri.OriginalString); + } + + return s; + } + } + + private static Uri GetUriFor(Type type) + { + if (type.Namespace != null) + { + var toRemove = type.Assembly.GetName().Name; + var substracted = toRemove.Length < type.Namespace.Length ? type.Namespace.Remove(0, toRemove.Length + 1) : ""; + var replace = substracted.Replace('.', Path.PathSeparator); + if (replace != string.Empty) + { + replace = replace + "/"; + } + return new Uri(replace + type.Name + ".xaml", UriKind.Relative); + } + + return null; + } + + public Type GetTypeFor(Uri uri) + { + var withExt = uri.OriginalString; + var lastSlash = withExt.LastIndexOf("/", StringComparison.Ordinal); + var innerNs = withExt.Substring(0, lastSlash); + var fileName = withExt.Substring(lastSlash + 1, withExt.Length - lastSlash - 1); + + var className = fileName.Substring(0, fileName.LastIndexOf('.')); + + + var assemblies = AppDomain.CurrentDomain.GetAssemblies().ToList(); + var type = from assembly in assemblies + let t = assembly.GetType(GetName(assembly, innerNs, className)) + where t != null + select t; + + return type.First(); + } + + private static string GetName(Assembly assembly, string innerNs, string className) + { + var ns = assembly.GetName().Name + "." + innerNs; + var fullLocator = ns + "." + className; + return fullLocator; + } + } +} \ No newline at end of file diff --git a/Perspex.Xaml.Desktop/Perspex.Xaml.Desktop.csproj b/Perspex.Xaml.Desktop/Perspex.Xaml.Desktop.csproj index 19b9ee7e77..9c4425267e 100644 --- a/Perspex.Xaml.Desktop/Perspex.Xaml.Desktop.csproj +++ b/Perspex.Xaml.Desktop/Perspex.Xaml.Desktop.csproj @@ -126,7 +126,7 @@ - + diff --git a/Perspex.Xaml.Desktop/PerspexInflatableTypeFactory.cs b/Perspex.Xaml.Desktop/PerspexInflatableTypeFactory.cs index 384a6dc513..17f6b2dfb6 100644 --- a/Perspex.Xaml.Desktop/PerspexInflatableTypeFactory.cs +++ b/Perspex.Xaml.Desktop/PerspexInflatableTypeFactory.cs @@ -9,7 +9,7 @@ namespace Perspex.Xaml.Desktop public class PerspexInflatableTypeFactory : InflatableTypeFactory { - public PerspexInflatableTypeFactory() : base(new TypeFactory(), new InflatableTranslator(), typeFactory => new PerspexXamlLoader(typeFactory)) + public PerspexInflatableTypeFactory() : base(new TypeFactory(), new InflatableResourceTranslator(), typeFactory => new PerspexXamlLoader(typeFactory)) { Inflatables = new Collection { typeof(Window), typeof(UserControl) }; } diff --git a/Perspex.Xaml.Desktop/WindowsResourceProvider.cs b/Perspex.Xaml.Desktop/WindowsResourceProvider.cs deleted file mode 100644 index a249b758e1..0000000000 --- a/Perspex.Xaml.Desktop/WindowsResourceProvider.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace Perspex.Xaml.Desktop -{ - using System; - using System.IO; - using System.Reflection; - using HighLevel; - - public class WindowsResourceProvider : IResourceProvider - { - public Stream GetStream(Uri uri) - { - var absoluteUri = new Uri(Assembly.GetExecutingAssembly().Location, UriKind.Absolute); - var finalUri = new Uri(absoluteUri, uri); - return new FileStream(finalUri.LocalPath, FileMode.Open); - } - } -} \ No newline at end of file diff --git a/XamlTestApplication/XamlTestApplication.csproj b/XamlTestApplication/XamlTestApplication.csproj index 8bf5a55ee7..1726163e51 100644 --- a/XamlTestApplication/XamlTestApplication.csproj +++ b/XamlTestApplication/XamlTestApplication.csproj @@ -201,10 +201,9 @@ - + Designer - PreserveNewest - +