Browse Source

Tweak IAssetLoader API and fix tests.

Steven Kirk 7 years ago
parent
commit
1ee4fa47f9

+ 15 - 1
src/Avalonia.Base/Platform/IAssetLoader.cs

@@ -44,6 +44,20 @@ namespace Avalonia.Platform
         /// </exception>
         Stream Open(Uri uri, Uri baseUri = null);
 
-        Tuple<Assembly, Stream> OpenWithAssembly(Uri uri, Uri baseUri = null);
+        /// <summary>
+        /// Opens the resource with the requested URI and returns the resource string and the
+        /// assembly containing the resource.
+        /// </summary>
+        /// <param name="uri">The URI.</param>
+        /// <param name="baseUri">
+        /// A base URI to use if <paramref name="uri"/> is relative.
+        /// </param>
+        /// <returns>
+        /// The stream containing the resource contents together with the assembly.
+        /// </returns>
+        /// <exception cref="FileNotFoundException">
+        /// The resource was not found.
+        /// </exception>
+        Tuple<Stream, Assembly> OpenAndGetAssembly(Uri uri, Uri baseUri = null);
     }
 }

+ 1 - 1
src/Markup/Avalonia.Markup.Xaml/Avalonia.Markup.Xaml.csproj

@@ -29,7 +29,7 @@
         <Compile Include="..\..\Shared\SharedAssemblyInfo.cs">
             <Link>Properties\SharedAssemblyInfo.cs</Link>
         </Compile>
-        <Compile Include="AvaloniaXamlLoaderPortableXaml.cs" />
+        <Compile Include="AvaloniaXamlLoader.cs" />
         <Compile Include="Converters\MatrixTypeConverter.cs" />
         <Compile Include="Converters\RectTypeConverter.cs" />
         <Compile Include="Converters\SetterValueTypeConverter.cs" />

+ 5 - 5
src/Markup/Avalonia.Markup.Xaml/AvaloniaXamlLoaderPortableXaml.cs → src/Markup/Avalonia.Markup.Xaml/AvaloniaXamlLoader.cs

@@ -125,12 +125,12 @@ namespace Avalonia.Markup.Xaml
                     "Could not create IAssetLoader : maybe Application.RegisterServices() wasn't called?");
             }
 
-            var asset = assetLocator.OpenWithAssembly(uri, baseUri);
-            using (var stream = asset.Item2)
+            var asset = assetLocator.OpenAndGetAssembly(uri, baseUri);
+            using (var stream = asset.Item1)
             {
                 try
                 {
-                    return Load(stream, asset.Item1, rootInstance, uri);
+                    return Load(stream, asset.Item2, rootInstance, uri);
                 }
                 catch (Exception e)
                 {
@@ -153,7 +153,7 @@ namespace Avalonia.Markup.Xaml
         /// The optional instance into which the XAML should be loaded.
         /// </param>
         /// <returns>The loaded object.</returns>
-        public object Load(string xaml, Assembly localAssembly, object rootInstance = null)
+        public object Load(string xaml, Assembly localAssembly = null, object rootInstance = null)
         {
             Contract.Requires<ArgumentNullException>(xaml != null);
 
@@ -230,7 +230,7 @@ namespace Avalonia.Markup.Xaml
         public static object Parse(string xaml, Assembly localAssembly = null)
             => new AvaloniaXamlLoader().Load(xaml, localAssembly);
 
-        public static T Parse<T>(string xaml, Assembly localAssembly)
+        public static T Parse<T>(string xaml, Assembly localAssembly = null)
             => (T)Parse(xaml, localAssembly);
     }
 }

+ 8 - 5
src/Shared/PlatformSupport/AssetLoader.cs

@@ -67,20 +67,23 @@ namespace Avalonia.Shared.PlatformSupport
         /// <exception cref="FileNotFoundException">
         /// The resource was not found.
         /// </exception>
-        public Stream Open(Uri uri, Uri baseUri = null) => OpenWithAssembly(uri, baseUri).Item2;
+        public Stream Open(Uri uri, Uri baseUri = null) => OpenAndGetAssembly(uri, baseUri).Item1;
         
         /// <summary>
-        /// Opens the resource with the requested URI.
+        /// Opens the resource with the requested URI and returns the resource string and the
+        /// assembly containing the resource.
         /// </summary>
         /// <param name="uri">The URI.</param>
         /// <param name="baseUri">
         /// A base URI to use if <paramref name="uri"/> is relative.
         /// </param>
-        /// <returns>An assembly (optional) and a stream containing the resource contents.</returns>
+        /// <returns>
+        /// The stream containing the resource contents together with the assembly.
+        /// </returns>
         /// <exception cref="FileNotFoundException">
         /// The resource was not found.
         /// </exception>
-        public Tuple<Assembly, Stream> OpenWithAssembly(Uri uri, Uri baseUri = null)
+        public Tuple<Stream, Assembly> OpenAndGetAssembly(Uri uri, Uri baseUri = null)
         {
             var asset = GetAsset(uri, baseUri);
 
@@ -89,7 +92,7 @@ namespace Avalonia.Shared.PlatformSupport
                 throw new FileNotFoundException($"The resource {uri} could not be found.");
             }
 
-            return Tuple.Create(asset.Assembly, asset.GetStream());
+            return Tuple.Create(asset.GetStream(), asset.Assembly);
         }
 
         private IAssetDescriptor GetAsset(Uri uri, Uri baseUri)

+ 2 - 2
tests/Avalonia.UnitTests/MockAssetLoader.cs

@@ -28,9 +28,9 @@ namespace Avalonia.UnitTests
             return new MemoryStream(Encoding.UTF8.GetBytes(_assets[uri]));
         }
         
-        public Tuple<Assembly, Stream> OpenWithAssembly(Uri uri, Uri baseUri = null)
+        public Tuple<Stream, Assembly> OpenAndGetAssembly(Uri uri, Uri baseUri = null)
         {
-            return Tuple.Create((Assembly) null, Open(uri, baseUri));
+            return Tuple.Create(Open(uri, baseUri), (Assembly)null);
         }
 
         public void SetDefaultAssembly(Assembly asm)