10 changed files with 142 additions and 36 deletions
@ -0,0 +1,43 @@ |
|||
// -----------------------------------------------------------------------
|
|||
// <copyright file="PerspexXamlLoader.cs" company="Steven Kirk">
|
|||
// Copyright 2014 MIT Licence. See licence.md for more information.
|
|||
// </copyright>
|
|||
// -----------------------------------------------------------------------
|
|||
|
|||
namespace Perspex.Xaml |
|||
{ |
|||
using System; |
|||
using OmniXaml; |
|||
using Platform; |
|||
using Perspex.Xaml.Context; |
|||
using Splat; |
|||
|
|||
public class PerspexXamlLoader : XamlLoader |
|||
{ |
|||
public PerspexXamlLoader() |
|||
: this(new PerspexParserFactory()) |
|||
{ |
|||
} |
|||
|
|||
public PerspexXamlLoader(IXamlParserFactory xamlParserFactory) |
|||
: base(xamlParserFactory) |
|||
{ |
|||
} |
|||
|
|||
public object Load(Uri uri, object rootInstance = null) |
|||
{ |
|||
var assetLocator = Locator.Current.GetService<IAssetLoader>(); |
|||
|
|||
if (assetLocator == null) |
|||
{ |
|||
throw new InvalidOperationException( |
|||
"Could not create IAssetLoader : maybe Application.RegisterServices() wasn't called?"); |
|||
} |
|||
|
|||
using (var stream = assetLocator.Open(uri)) |
|||
{ |
|||
return this.Load(stream, rootInstance); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
// -----------------------------------------------------------------------
|
|||
// <copyright file="IAssetLoader.cs" company="Steven Kirk">
|
|||
// Copyright 2015 MIT Licence. See licence.md for more information.
|
|||
// </copyright>
|
|||
// -----------------------------------------------------------------------
|
|||
|
|||
namespace Perspex.Platform |
|||
{ |
|||
using System; |
|||
using System.IO; |
|||
|
|||
/// <summary>
|
|||
/// Loads assets compiled into the application binary.
|
|||
/// </summary>
|
|||
public interface IAssetLoader |
|||
{ |
|||
/// <summary>
|
|||
/// Opens the resource with the requested URI.
|
|||
/// </summary>
|
|||
/// <param name="uri">The URI.</param>
|
|||
/// <returns>A stream containing the resource contents.</returns>
|
|||
/// <exception cref="FileNotFoundException">
|
|||
/// The resource was not found.
|
|||
/// </exception>
|
|||
Stream Open(Uri uri); |
|||
} |
|||
} |
|||
@ -0,0 +1,48 @@ |
|||
// -----------------------------------------------------------------------
|
|||
// <copyright file="AssetLoader.cs" company="Steven Kirk">
|
|||
// Copyright 2015 MIT Licence. See licence.md for more information.
|
|||
// </copyright>
|
|||
// -----------------------------------------------------------------------
|
|||
|
|||
namespace Perspex.Win32 |
|||
{ |
|||
using System; |
|||
using System.Globalization; |
|||
using System.IO; |
|||
using System.Reflection; |
|||
using System.Resources; |
|||
using Perspex.Platform; |
|||
|
|||
/// <summary>
|
|||
/// Loads assets compiled into the application binary.
|
|||
/// </summary>
|
|||
public class AssetLoader : IAssetLoader |
|||
{ |
|||
/// <summary>
|
|||
/// Opens the resource with the requested URI.
|
|||
/// </summary>
|
|||
/// <param name="uri">The URI.</param>
|
|||
/// <returns>A stream containing the resource contents.</returns>
|
|||
/// <exception cref="FileNotFoundException">
|
|||
/// The resource was not found.
|
|||
/// </exception>
|
|||
public Stream Open(Uri uri) |
|||
{ |
|||
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(uri.ToString(), true); |
|||
|
|||
if (stream == null) |
|||
{ |
|||
throw new FileNotFoundException($"The requested asset could not be found: {uri}"); |
|||
} |
|||
|
|||
return stream; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue