Browse Source

Tweak IAssetLoader API and fix tests.

pull/1389/head
Steven Kirk 8 years ago
parent
commit
1ee4fa47f9
  1. 16
      src/Avalonia.Base/Platform/IAssetLoader.cs
  2. 2
      src/Markup/Avalonia.Markup.Xaml/Avalonia.Markup.Xaml.csproj
  3. 10
      src/Markup/Avalonia.Markup.Xaml/AvaloniaXamlLoader.cs
  4. 13
      src/Shared/PlatformSupport/AssetLoader.cs
  5. 4
      tests/Avalonia.UnitTests/MockAssetLoader.cs

16
src/Avalonia.Base/Platform/IAssetLoader.cs

@ -44,6 +44,20 @@ namespace Avalonia.Platform
/// </exception> /// </exception>
Stream Open(Uri uri, Uri baseUri = null); 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);
} }
} }

2
src/Markup/Avalonia.Markup.Xaml/Avalonia.Markup.Xaml.csproj

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

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

13
src/Shared/PlatformSupport/AssetLoader.cs

@ -67,20 +67,23 @@ namespace Avalonia.Shared.PlatformSupport
/// <exception cref="FileNotFoundException"> /// <exception cref="FileNotFoundException">
/// The resource was not found. /// The resource was not found.
/// </exception> /// </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> /// <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> /// </summary>
/// <param name="uri">The URI.</param> /// <param name="uri">The URI.</param>
/// <param name="baseUri"> /// <param name="baseUri">
/// A base URI to use if <paramref name="uri"/> is relative. /// A base URI to use if <paramref name="uri"/> is relative.
/// </param> /// </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"> /// <exception cref="FileNotFoundException">
/// The resource was not found. /// The resource was not found.
/// </exception> /// </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); var asset = GetAsset(uri, baseUri);
@ -89,7 +92,7 @@ namespace Avalonia.Shared.PlatformSupport
throw new FileNotFoundException($"The resource {uri} could not be found."); 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) private IAssetDescriptor GetAsset(Uri uri, Uri baseUri)

4
tests/Avalonia.UnitTests/MockAssetLoader.cs

@ -28,9 +28,9 @@ namespace Avalonia.UnitTests
return new MemoryStream(Encoding.UTF8.GetBytes(_assets[uri])); 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) public void SetDefaultAssembly(Assembly asm)

Loading…
Cancel
Save