From 70a9ba81a5b15d5328fcb666d614c092467cf2ee Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 28 Aug 2015 23:19:06 +0200 Subject: [PATCH] Added AssetLoader to Perspex.Gtk. --- src/Gtk/Perspex.Gtk/AssetLoader.cs | 48 ++++++++++++++++++++++++++ src/Gtk/Perspex.Gtk/GtkPlatform.cs | 1 + src/Gtk/Perspex.Gtk/Perspex.Gtk.csproj | 5 +++ 3 files changed, 54 insertions(+) create mode 100644 src/Gtk/Perspex.Gtk/AssetLoader.cs diff --git a/src/Gtk/Perspex.Gtk/AssetLoader.cs b/src/Gtk/Perspex.Gtk/AssetLoader.cs new file mode 100644 index 0000000000..43276dac0b --- /dev/null +++ b/src/Gtk/Perspex.Gtk/AssetLoader.cs @@ -0,0 +1,48 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2015 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.Gtk +{ + using System; + using System.Globalization; + using System.IO; + using System.Reflection; + using System.Resources; + using Perspex.Platform; + + /// + /// Loads assets compiled into the application binary. + /// + public class AssetLoader : IAssetLoader + { + /// + /// Opens the resource with the requested URI. + /// + /// The URI. + /// A stream containing the resource contents. + /// + /// The resource was not found. + /// + 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; + } + } + } +} diff --git a/src/Gtk/Perspex.Gtk/GtkPlatform.cs b/src/Gtk/Perspex.Gtk/GtkPlatform.cs index 7618fb8f3c..7165046926 100644 --- a/src/Gtk/Perspex.Gtk/GtkPlatform.cs +++ b/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() diff --git a/src/Gtk/Perspex.Gtk/Perspex.Gtk.csproj b/src/Gtk/Perspex.Gtk/Perspex.Gtk.csproj index 858396a5cc..ba89098092 100644 --- a/src/Gtk/Perspex.Gtk/Perspex.Gtk.csproj +++ b/src/Gtk/Perspex.Gtk/Perspex.Gtk.csproj @@ -58,6 +58,7 @@ + @@ -72,6 +73,10 @@ {d211e587-d8bc-45b9-95a4-f297c8fa5200} Perspex.Animation + + {799a7bb5-3c2c-48b6-85a7-406a12c420da} + Perspex.Application + {B09B78D8-9B26-48B0-9149-D64A2F120F3F} Perspex.Base