Browse Source

Merge pull request #81 from SuperJMN/alpha1

Fixed resource loading. Deleted PerspexXamlLoader (useless).
Steven Kirk 10 years ago
parent
commit
2375c7f714

+ 29 - 2
samples/XamlTestApplication/Views/MainWindow.cs

@@ -1,5 +1,11 @@
 namespace XamlTestApplication.Views
 {
+    using System;
+    using System.Globalization;
+    using System.IO;
+    using System.Reflection;
+    using System.Resources;
+    using OmniXaml;
     using OmniXaml.AppServices.Mvvm;
     using Perspex.Diagnostics;
     using Perspex.Xaml.Desktop;
@@ -16,8 +22,29 @@
 
         private void InitializeComponent()
         {
-            var loader = new PerspexXamlLoader(new PerspexInflatableTypeFactory());
-            loader.Load(this.GetType());
+            var xamlLoader = new XamlLoader(new PerspexParserFactory(new TypeFactory()));
+            var stream = GetStream(new Uri("Views/MainWindow.xaml", UriKind.Relative));
+            xamlLoader.Load(stream, this);
+        }
+
+
+        private static Stream GetStream(Uri resourceLocator)
+        {
+            var assembly = Assembly.GetEntryAssembly();
+            var resourceName = assembly.GetName().Name + ".g";
+            var manager = new ResourceManager(resourceName, assembly);
+
+            using (var resourceSet = manager.GetResourceSet(CultureInfo.CurrentCulture, true, true))
+            {
+                var stream = (Stream)resourceSet.GetObject(resourceLocator.ToString(), true);
+
+                if (stream == null)
+                {
+                    throw new IOException($"The requested resource could not be found: {resourceLocator}");
+                }
+
+                return stream;
+            }
         }
     }
 }

+ 0 - 2
src/Markup/Perspex.Xaml.Desktop/Perspex.Xaml.Desktop.csproj

@@ -121,10 +121,8 @@
     <Compile Include="..\..\Shared\SharedAssemblyInfo.cs">
       <Link>Properties\SharedAssemblyInfo.cs</Link>
     </Compile>
-    <Compile Include="PerspexInflatableTypeFactory.cs" />
     <Compile Include="PerspexParserFactory.cs" />
     <Compile Include="PerspexWindow.cs" />
-    <Compile Include="PerspexXamlLoader.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="InflatableResourceTranslator.cs" />
   </ItemGroup>

+ 0 - 17
src/Markup/Perspex.Xaml.Desktop/PerspexInflatableTypeFactory.cs

@@ -1,17 +0,0 @@
-namespace Perspex.Xaml.Desktop
-{
-    using System;
-    using System.Collections.ObjectModel;
-    using Controls;
-    using OmniXaml;
-    using OmniXaml.AppServices;
-    using OmniXaml.AppServices.NetCore;
-
-    public class PerspexInflatableTypeFactory : InflatableTypeFactory
-    {
-        public PerspexInflatableTypeFactory() : base(new TypeFactory(), new InflatableTranslator(), typeFactory => new PerspexXamlLoader(typeFactory))
-        {
-            Inflatables = new Collection<Type> { typeof(Window), typeof(UserControl) };
-        }
-    }
-}

+ 0 - 65
src/Markup/Perspex.Xaml.Desktop/PerspexXamlLoader.cs

@@ -1,65 +0,0 @@
-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()
-            : 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;
-        }
-    }
-}