Browse Source

Try to do simple XAML loading.

Doesn't yet work - seems x:Class isn't supported by OmniXaml.
Steven Kirk 10 years ago
parent
commit
279f9abab0

+ 2 - 6
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);
         }      

+ 8 - 0
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());
+        }
     }
 }

+ 4 - 1
samples/XamlTestApplication/Views/MainWindow.xaml

@@ -1,4 +1,7 @@
-<Window Title="Perspex Test Application" Height="350" Width="525" xmlns="https://github.com/grokys/Perspex">
+<Window x:Class="XamlTestApplication.MainWindow"
+        xmlns="https://github.com/grokys/Perspex"
+        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
+        Title="Perspex Test Application" Height="350" Width="525">
     <Grid>
         <Grid.RowDefinitions>
             <RowDefinition Height="Auto" />

+ 1 - 1
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<Type> { typeof(Window), typeof(UserControl) };
         }

+ 55 - 1
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;
+        }
     }
 }