Browse Source

Added AssetLoader to Perspex.Gtk.

pull/83/head
Steven Kirk 11 years ago
parent
commit
70a9ba81a5
  1. 48
      src/Gtk/Perspex.Gtk/AssetLoader.cs
  2. 1
      src/Gtk/Perspex.Gtk/GtkPlatform.cs
  3. 5
      src/Gtk/Perspex.Gtk/Perspex.Gtk.csproj

48
src/Gtk/Perspex.Gtk/AssetLoader.cs

@ -0,0 +1,48 @@
// -----------------------------------------------------------------------
// <copyright file="AssetLoader.cs" company="Steven Kirk">
// Copyright 2015 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Gtk
{
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;
}
}
}
}

1
src/Gtk/Perspex.Gtk/GtkPlatform.cs

@ -44,6 +44,7 @@ namespace Perspex.Gtk
locator.Register(() => GtkKeyboardDevice.Instance, typeof(IKeyboardDevice));
locator.Register(() => instance, typeof(IPlatformSettings));
locator.Register(() => instance, typeof(IPlatformThreadingInterface));
locator.RegisterConstant(new AssetLoader(), typeof(IAssetLoader));
}
public bool HasMessages()

5
src/Gtk/Perspex.Gtk/Perspex.Gtk.csproj

@ -58,6 +58,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="AssetLoader.cs" />
<Compile Include="GtkExtensions.cs" />
<Compile Include="PopupImpl.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
@ -72,6 +73,10 @@
<Project>{d211e587-d8bc-45b9-95a4-f297c8fa5200}</Project>
<Name>Perspex.Animation</Name>
</ProjectReference>
<ProjectReference Include="..\..\Perspex.Application\Perspex.Application.csproj">
<Project>{799a7bb5-3c2c-48b6-85a7-406a12c420da}</Project>
<Name>Perspex.Application</Name>
</ProjectReference>
<ProjectReference Include="..\..\Perspex.Base\Perspex.Base.csproj">
<Project>{B09B78D8-9B26-48B0-9149-D64A2F120F3F}</Project>
<Name>Perspex.Base</Name>

Loading…
Cancel
Save