Explorar o código

minor refactorings

Andrey Kunchev %!s(int64=8) %!d(string=hai) anos
pai
achega
1f530c20f4

+ 4 - 5
src/Markup/Avalonia.Markup.Xaml/AvaloniaXamlLoaderPortableXaml.cs

@@ -168,7 +168,9 @@ namespace Avalonia.Markup.Xaml
 
             var reader = new XamlXmlReader(stream, _context, readerSettings);
 
-            object result = LoadFromReader(reader, rootInstance, readerSettings);
+            object result = LoadFromReader(
+                reader,
+                AvaloniaXamlContext.For(readerSettings, rootInstance));
 
             var topLevel = result as TopLevel;
 
@@ -180,13 +182,10 @@ namespace Avalonia.Markup.Xaml
             return result;
         }
 
-        internal static object LoadFromReader(XamlReader reader,
-                        object instance,
-                        AvaloniaXamlContext context = null)
+        internal static object LoadFromReader(XamlReader reader, AvaloniaXamlContext context = null)
         {
             var writer = AvaloniaXamlObjectWriter.Create(
                                     reader.SchemaContext,
-                                    instance,
                                     context);
 
             XamlServices.Transform(reader, writer);

+ 15 - 8
src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaXamlContext.cs

@@ -1,24 +1,31 @@
 using Portable.Xaml;
+using Portable.Xaml.Markup;
 using System;
 using System.Reflection;
 
 namespace Avalonia.Markup.Xaml.PortableXaml
 {
-    public class AvaloniaXamlContext
+    public class AvaloniaXamlContext : IUriContext
     {
-        internal AvaloniaXamlContext(Uri baseUri, Assembly localAssembly)
+        private AvaloniaXamlContext()
         {
-            LocalAssembly = localAssembly;
-            BaseUri = baseUri;
         }
 
-        public Assembly LocalAssembly { get; }
+        public Assembly LocalAssembly { get; private set; }
 
-        public Uri BaseUri { get; }
+        public Uri BaseUri { get; set; }
 
-        public static implicit operator AvaloniaXamlContext(XamlXmlReaderSettings sett)
+        public object RootInstance { get; private set; }
+
+        internal static AvaloniaXamlContext For(XamlXmlReaderSettings sett,
+                                                object rootInstance)
         {
-            return new AvaloniaXamlContext(sett.BaseUri, sett.LocalAssembly);
+            return new AvaloniaXamlContext()
+            {
+                BaseUri = sett.BaseUri,
+                LocalAssembly = sett.LocalAssembly,
+                RootInstance = rootInstance
+            };
         }
     }
 }

+ 2 - 3
src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaXamlObjectWriter.cs

@@ -10,16 +10,15 @@ namespace Avalonia.Markup.Xaml.PortableXaml
     {
         public static AvaloniaXamlObjectWriter Create(
             XamlSchemaContext schemaContext,
-            object instance,
             AvaloniaXamlContext context)
         {
-            var nameScope = new AvaloniaNameScope { Instance = instance };
+            var nameScope = new AvaloniaNameScope { Instance = context?.RootInstance };
 
             var writerSettings = new XamlObjectWriterSettings()
             {
                 ExternalNameScope = nameScope,
                 RegisterNamesOnExternalNamescope = true,
-                RootObjectInstance = instance
+                RootObjectInstance = context?.RootInstance
             };
 
             return new AvaloniaXamlObjectWriter(schemaContext,

+ 11 - 6
tests/Avalonia.Markup.Xaml.UnitTests/Xaml/StyleTests.cs

@@ -231,21 +231,26 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
         [Fact]
         public void StyleInclude_Is_Built()
         {
-            var xaml = @"
+            using (UnitTestApplication.Start(TestServices.StyledWindow
+                                              .With(theme: () => new Styles())))
+            {
+                var xaml = @"
 <ContentControl xmlns='https://github.com/avaloniaui'>
     <ContentControl.Styles>
         <StyleInclude Source='resm:Avalonia.Markup.Xaml.UnitTests.Xaml.Style1.xaml?assembly=Avalonia.Markup.Xaml.UnitTests'/>
     </ContentControl.Styles>
 </ContentControl>";
 
-            var window = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
+                var window = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
 
-            Assert.Equal(1, window.Styles.Count);
+                Assert.Equal(1, window.Styles.Count);
 
-            var styleInclude = window.Styles[0] as StyleInclude;
+                var styleInclude = window.Styles[0] as StyleInclude;
 
-            Assert.NotNull(styleInclude);
-            Assert.NotNull(styleInclude.Source);
+                Assert.NotNull(styleInclude);
+                Assert.NotNull(styleInclude.Source);
+                Assert.NotNull(styleInclude.Loaded);
+            }
         }
 
         [Fact]