Browse Source

Add IAssetLoader.Exists

Closes #181.
Steven Kirk 10 years ago
parent
commit
5a27ecf264

+ 5 - 9
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);
         }

+ 7 - 0
src/Perspex.Application/Platform/IAssetLoader.cs

@@ -11,6 +11,13 @@ namespace Perspex.Platform
     /// </summary>
     public interface IAssetLoader
     {
+        /// <summary>
+        /// Checks if an asset with the specified URI exists.
+        /// </summary>
+        /// <param name="uri">The URI.</param>
+        /// <returns>True if the asset could be found; otherwise false.</returns>
+        bool Exists(Uri uri);
+
         /// <summary>
         /// Opens the resource with the requested URI.
         /// </summary>

+ 15 - 4
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<string, Assembly> AssemblyNameCache
             = new Dictionary<string, Assembly>();
 
-
         static Assembly GetAssembly(string name)
         {
             Assembly rv;
@@ -31,6 +28,20 @@ namespace Perspex.Shared.PlatformSupport
             return rv;
         }
 
+        /// <summary>
+        /// Checks if an asset with the specified URI exists.
+        /// </summary>
+        /// <param name="uri">The URI.</param>
+        /// <returns>True if the asset could be found; otherwise false.</returns>
+        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;
+        }
+
         /// <summary>
         /// Opens the resource with the requested URI.
         /// </summary>
@@ -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;
         }
     }