From 5a27ecf2642e73d3c218304564d74d25ad36cd63 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 24 Sep 2015 01:58:27 +0200 Subject: [PATCH] Add IAssetLoader.Exists Closes #181. --- .../Perspex.Markup.Xaml/PerspexXamlLoader.cs | 14 +++++--------- .../Platform/IAssetLoader.cs | 7 +++++++ src/Shared/PlatformSupport/AssetLoader.cs | 19 +++++++++++++++---- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/Markup/Perspex.Markup.Xaml/PerspexXamlLoader.cs b/src/Markup/Perspex.Markup.Xaml/PerspexXamlLoader.cs index e69bcfdf81..65e1ce4f71 100644 --- a/src/Markup/Perspex.Markup.Xaml/PerspexXamlLoader.cs +++ b/src/Markup/Perspex.Markup.Xaml/PerspexXamlLoader.cs @@ -65,17 +65,13 @@ namespace Perspex.Markup.Xaml } foreach (var uri in GetUrisFor(type)) { - Stream stream; - try + if (assetLocator.Exists(uri)) { - stream= assetLocator.Open(uri); + using (var stream = assetLocator.Open(uri)) + { + return Load(stream, rootInstance); + } } - catch (FileNotFoundException) - { - continue; - } - using (stream) - return Load(stream, rootInstance); } throw new FileNotFoundException("Unable to find view for " + type.FullName); } diff --git a/src/Perspex.Application/Platform/IAssetLoader.cs b/src/Perspex.Application/Platform/IAssetLoader.cs index 96e0aad475..9594e7e021 100644 --- a/src/Perspex.Application/Platform/IAssetLoader.cs +++ b/src/Perspex.Application/Platform/IAssetLoader.cs @@ -11,6 +11,13 @@ namespace Perspex.Platform /// public interface IAssetLoader { + /// + /// Checks if an asset with the specified URI exists. + /// + /// The URI. + /// True if the asset could be found; otherwise false. + bool Exists(Uri uri); + /// /// Opens the resource with the requested URI. /// diff --git a/src/Shared/PlatformSupport/AssetLoader.cs b/src/Shared/PlatformSupport/AssetLoader.cs index 3be0509ef4..229cefb8b2 100644 --- a/src/Shared/PlatformSupport/AssetLoader.cs +++ b/src/Shared/PlatformSupport/AssetLoader.cs @@ -3,11 +3,9 @@ using System; using System.Collections.Generic; -using System.Globalization; using System.IO; using System.Linq; using System.Reflection; -using System.Resources; using Perspex.Platform; namespace Perspex.Shared.PlatformSupport @@ -20,7 +18,6 @@ namespace Perspex.Shared.PlatformSupport private static readonly Dictionary AssemblyNameCache = new Dictionary(); - static Assembly GetAssembly(string name) { Assembly rv; @@ -31,6 +28,20 @@ namespace Perspex.Shared.PlatformSupport return rv; } + /// + /// Checks if an asset with the specified URI exists. + /// + /// The URI. + /// True if the asset could be found; otherwise false. + public bool Exists(Uri uri) + { + var parts = uri.AbsolutePath.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); + var asm = parts.Length == 1 ? Assembly.GetEntryAssembly() : GetAssembly(parts[0]); + var typeName = parts[parts.Length == 1 ? 0 : 1]; + var rv = asm.GetManifestResourceStream(typeName); + return rv != null; + } + /// /// Opens the resource with the requested URI. /// @@ -46,7 +57,7 @@ namespace Perspex.Shared.PlatformSupport var typeName = parts[parts.Length == 1 ? 0 : 1]; var rv = asm.GetManifestResourceStream(typeName); if (rv == null) - throw new FileNotFoundException(uri.ToString()); + throw new FileNotFoundException($"The resource {uri} could not be found."); return rv; } }