csharpc-sharpdotnetxamlavaloniauicross-platformcross-platform-xamlavaloniaguimulti-platformuser-interfacedotnetcore
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.6 KiB
48 lines
1.6 KiB
// -----------------------------------------------------------------------
|
|
// <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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|