From 56b88d287ccfababbbab666261354292ac71fa88 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 28 Aug 2015 13:55:55 +0200 Subject: [PATCH] Refactored loading assets into IAssetLoader. --- samples/XamlTestApplication/Program.cs | 11 +---- .../XamlTestApplication/Views/MainWindow.cs | 27 ++--------- .../{ => Context}/PerspexParserFactory.cs | 7 ++- src/Markup/Perspex.Xaml/Perspex.Xaml.csproj | 7 ++- src/Markup/Perspex.Xaml/PerspexXamlLoader.cs | 43 +++++++++++++++++ .../Perspex.Application.csproj | 1 + .../Platform/IAssetLoader.cs | 27 +++++++++++ src/Windows/Perspex.Win32/AssetLoader.cs | 48 +++++++++++++++++++ .../Perspex.Win32/Perspex.Win32.csproj | 5 ++ src/Windows/Perspex.Win32/Win32Platform.cs | 2 + 10 files changed, 142 insertions(+), 36 deletions(-) rename src/Markup/Perspex.Xaml/{ => Context}/PerspexParserFactory.cs (93%) create mode 100644 src/Markup/Perspex.Xaml/PerspexXamlLoader.cs create mode 100644 src/Perspex.Application/Platform/IAssetLoader.cs create mode 100644 src/Windows/Perspex.Win32/AssetLoader.cs diff --git a/samples/XamlTestApplication/Program.cs b/samples/XamlTestApplication/Program.cs index 6d549f2fac..e24f228086 100644 --- a/samples/XamlTestApplication/Program.cs +++ b/samples/XamlTestApplication/Program.cs @@ -1,21 +1,12 @@ -#if PERSPEX_GTK -using Perspex.Gtk; -#endif - -namespace XamlTestApplication +namespace XamlTestApplication { using System; using System.Diagnostics; using System.Windows.Threading; - using Glass; - using OmniXaml.AppServices.Mvvm; - using OmniXaml.AppServices.NetCore; using Perspex; using Perspex.Collections; using Perspex.Controls; using Perspex.Controls.Templates; - using Perspex.Input; - using Perspex.Xaml.Desktop; using ReactiveUI; using Views; diff --git a/samples/XamlTestApplication/Views/MainWindow.cs b/samples/XamlTestApplication/Views/MainWindow.cs index e6053ce51b..2f0dc53518 100644 --- a/samples/XamlTestApplication/Views/MainWindow.cs +++ b/samples/XamlTestApplication/Views/MainWindow.cs @@ -8,7 +8,7 @@ using OmniXaml; using Perspex.Controls; using Perspex.Diagnostics; - using Perspex.Xaml.Desktop; + using Perspex.Xaml; public class MainWindow : Window { @@ -21,29 +21,8 @@ private void InitializeComponent() { - var xamlLoader = new XamlLoader(new PerspexParserFactory(new TypeFactory())); - var stream = GetStream(new Uri("Views/MainWindow.xaml", UriKind.Relative)); - xamlLoader.Load(stream, this); - } - - - private static Stream GetStream(Uri resourceLocator) - { - 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(resourceLocator.ToString(), true); - - if (stream == null) - { - throw new IOException($"The requested resource could not be found: {resourceLocator}"); - } - - return stream; - } + var xamlLoader = new PerspexXamlLoader(); + xamlLoader.Load(new Uri("Views/MainWindow.xaml", UriKind.Relative), this); } } } \ No newline at end of file diff --git a/src/Markup/Perspex.Xaml/PerspexParserFactory.cs b/src/Markup/Perspex.Xaml/Context/PerspexParserFactory.cs similarity index 93% rename from src/Markup/Perspex.Xaml/PerspexParserFactory.cs rename to src/Markup/Perspex.Xaml/Context/PerspexParserFactory.cs index 28617ea20a..c89509f7d2 100644 --- a/src/Markup/Perspex.Xaml/PerspexParserFactory.cs +++ b/src/Markup/Perspex.Xaml/Context/PerspexParserFactory.cs @@ -1,4 +1,4 @@ -namespace Perspex.Xaml.Desktop +namespace Perspex.Xaml.Context { using OmniXaml; using OmniXaml.ObjectAssembler; @@ -10,6 +10,11 @@ namespace Perspex.Xaml.Desktop { private readonly IWiringContext wiringContext; + public PerspexParserFactory() + : this(new TypeFactory()) + { + } + public PerspexParserFactory(ITypeFactory typeFactory) { wiringContext = new PerspexWiringContext(typeFactory); diff --git a/src/Markup/Perspex.Xaml/Perspex.Xaml.csproj b/src/Markup/Perspex.Xaml/Perspex.Xaml.csproj index aca6e00e59..f5f7a7c1c4 100644 --- a/src/Markup/Perspex.Xaml/Perspex.Xaml.csproj +++ b/src/Markup/Perspex.Xaml/Perspex.Xaml.csproj @@ -47,7 +47,7 @@ - + @@ -66,6 +66,7 @@ + @@ -76,6 +77,10 @@ {d211e587-d8bc-45b9-95a4-f297c8fa5200} Perspex.Animation + + {799a7bb5-3c2c-48b6-85a7-406a12c420da} + Perspex.Application + {B09B78D8-9B26-48B0-9149-D64A2F120F3F} Perspex.Base diff --git a/src/Markup/Perspex.Xaml/PerspexXamlLoader.cs b/src/Markup/Perspex.Xaml/PerspexXamlLoader.cs new file mode 100644 index 0000000000..b6bf6e44fa --- /dev/null +++ b/src/Markup/Perspex.Xaml/PerspexXamlLoader.cs @@ -0,0 +1,43 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2014 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +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(); + + 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); + } + } + } +} diff --git a/src/Perspex.Application/Perspex.Application.csproj b/src/Perspex.Application/Perspex.Application.csproj index 6f47a06bda..c378be1bee 100644 --- a/src/Perspex.Application/Perspex.Application.csproj +++ b/src/Perspex.Application/Perspex.Application.csproj @@ -72,6 +72,7 @@ Properties\SharedAssemblyInfo.cs + diff --git a/src/Perspex.Application/Platform/IAssetLoader.cs b/src/Perspex.Application/Platform/IAssetLoader.cs new file mode 100644 index 0000000000..3ac4abc994 --- /dev/null +++ b/src/Perspex.Application/Platform/IAssetLoader.cs @@ -0,0 +1,27 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2015 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.Platform +{ + using System; + using System.IO; + + /// + /// Loads assets compiled into the application binary. + /// + public interface IAssetLoader + { + /// + /// Opens the resource with the requested URI. + /// + /// The URI. + /// A stream containing the resource contents. + /// + /// The resource was not found. + /// + Stream Open(Uri uri); + } +} diff --git a/src/Windows/Perspex.Win32/AssetLoader.cs b/src/Windows/Perspex.Win32/AssetLoader.cs new file mode 100644 index 0000000000..1d0c008459 --- /dev/null +++ b/src/Windows/Perspex.Win32/AssetLoader.cs @@ -0,0 +1,48 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2015 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.Win32 +{ + 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/Windows/Perspex.Win32/Perspex.Win32.csproj b/src/Windows/Perspex.Win32/Perspex.Win32.csproj index 3f8486b194..7f83e98d5c 100644 --- a/src/Windows/Perspex.Win32/Perspex.Win32.csproj +++ b/src/Windows/Perspex.Win32/Perspex.Win32.csproj @@ -63,6 +63,7 @@ Properties\SharedAssemblyInfo.cs + @@ -81,6 +82,10 @@ {d211e587-d8bc-45b9-95a4-f297c8fa5200} Perspex.Animation + + {799a7bb5-3c2c-48b6-85a7-406a12c420da} + Perspex.Application + {B09B78D8-9B26-48B0-9149-D64A2F120F3F} Perspex.Base diff --git a/src/Windows/Perspex.Win32/Win32Platform.cs b/src/Windows/Perspex.Win32/Win32Platform.cs index 6ae2a65940..b96b9d5719 100644 --- a/src/Windows/Perspex.Win32/Win32Platform.cs +++ b/src/Windows/Perspex.Win32/Win32Platform.cs @@ -51,12 +51,14 @@ namespace Perspex.Win32 public static void Initialize() { var locator = Locator.CurrentMutable; + locator.Register(() => new PopupImpl(), typeof(IPopupImpl)); locator.Register(() => new WindowImpl(), typeof(IWindowImpl)); locator.Register(() => WindowsKeyboardDevice.Instance, typeof(IKeyboardDevice)); locator.Register(() => WindowsMouseDevice.Instance, typeof(IMouseDevice)); locator.Register(() => instance, typeof(IPlatformSettings)); locator.Register(() => instance, typeof(IPlatformThreadingInterface)); + locator.RegisterConstant(new AssetLoader(), typeof(IAssetLoader)); } public bool HasMessages()