diff --git a/samples/XamlTestApplication/Program.cs b/samples/XamlTestApplication/Program.cs index 5b8f3ba0ba..6d549f2fac 100644 --- a/samples/XamlTestApplication/Program.cs +++ b/samples/XamlTestApplication/Program.cs @@ -17,6 +17,7 @@ namespace XamlTestApplication using Perspex.Input; using Perspex.Xaml.Desktop; using ReactiveUI; + using Views; class Item { @@ -55,12 +56,7 @@ namespace XamlTestApplication var testCommand = ReactiveCommand.Create(); testCommand.Subscribe(_ => Debug.WriteLine("Test command executed.")); - var typeFactory = new PerspexInflatableTypeFactory(); - - var viewFactory = new ViewFactory(typeFactory); - viewFactory.RegisterViews(ViewRegistration.FromTypes(Assemblies.AssembliesInAppFolder.AllExportedTypes())); - - var window = (Window) viewFactory.GetWindow("Main"); + var window = new MainWindow(); window.Show(); Application.Current.Run(window); } diff --git a/samples/XamlTestApplication/Views/MainWindow.cs b/samples/XamlTestApplication/Views/MainWindow.cs index e3f19512be..4a7e814a91 100644 --- a/samples/XamlTestApplication/Views/MainWindow.cs +++ b/samples/XamlTestApplication/Views/MainWindow.cs @@ -9,7 +9,15 @@ { public MainWindow() { + this.InitializeComponent(); + DevTools.Attach(this); } + + private void InitializeComponent() + { + var loader = new PerspexXamlLoader(new PerspexInflatableTypeFactory()); + loader.Load(this.GetType()); + } } } \ No newline at end of file diff --git a/samples/XamlTestApplication/Views/MainWindow.xaml b/samples/XamlTestApplication/Views/MainWindow.xaml index ad313cccb6..3294edb8b0 100644 --- a/samples/XamlTestApplication/Views/MainWindow.xaml +++ b/samples/XamlTestApplication/Views/MainWindow.xaml @@ -1,4 +1,7 @@ - + diff --git a/src/Markup/Perspex.Xaml.Desktop/PerspexInflatableTypeFactory.cs b/src/Markup/Perspex.Xaml.Desktop/PerspexInflatableTypeFactory.cs index 17f6b2dfb6..384a6dc513 100644 --- a/src/Markup/Perspex.Xaml.Desktop/PerspexInflatableTypeFactory.cs +++ b/src/Markup/Perspex.Xaml.Desktop/PerspexInflatableTypeFactory.cs @@ -9,7 +9,7 @@ namespace Perspex.Xaml.Desktop public class PerspexInflatableTypeFactory : InflatableTypeFactory { - public PerspexInflatableTypeFactory() : base(new TypeFactory(), new InflatableResourceTranslator(), typeFactory => new PerspexXamlLoader(typeFactory)) + public PerspexInflatableTypeFactory() : base(new TypeFactory(), new InflatableTranslator(), typeFactory => new PerspexXamlLoader(typeFactory)) { Inflatables = new Collection { typeof(Window), typeof(UserControl) }; } diff --git a/src/Markup/Perspex.Xaml.Desktop/PerspexXamlLoader.cs b/src/Markup/Perspex.Xaml.Desktop/PerspexXamlLoader.cs index 2d2bb68f45..7e60585352 100644 --- a/src/Markup/Perspex.Xaml.Desktop/PerspexXamlLoader.cs +++ b/src/Markup/Perspex.Xaml.Desktop/PerspexXamlLoader.cs @@ -1,11 +1,65 @@ namespace Perspex.Xaml.Desktop { + using System; + using System.Globalization; + using System.IO; + using System.Reflection; + using System.Resources; using OmniXaml; public class PerspexXamlLoader : XamlLoader { - public PerspexXamlLoader(ITypeFactory typeFactory) : base(new PerspexParserFactory(typeFactory)) + public PerspexXamlLoader() + : this(new PerspexInflatableTypeFactory()) + { + } + + public PerspexXamlLoader(ITypeFactory typeFactory) + : base(new PerspexParserFactory(typeFactory)) { } + + public void Load(Type type) + { + this.Load(GetUriFor(type)); + } + + public void Load(string path) + { + 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)) + { + var s = (Stream)resourceSet.GetObject(path, true); + + if (s == null) + { + throw new IOException($"The requested resource could not be found: {path}"); + } + + this.Load(s); + } + } + + private static string 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 replace + type.Name + ".xaml"; + } + + return null; + } } } \ No newline at end of file