Browse Source

Add IAssetLoader.Exists

Closes #181.
pull/190/head
Steven Kirk 11 years ago
parent
commit
5a27ecf264
  1. 14
      src/Markup/Perspex.Markup.Xaml/PerspexXamlLoader.cs
  2. 7
      src/Perspex.Application/Platform/IAssetLoader.cs
  3. 19
      src/Shared/PlatformSupport/AssetLoader.cs

14
src/Markup/Perspex.Markup.Xaml/PerspexXamlLoader.cs

@ -65,17 +65,13 @@ namespace Perspex.Markup.Xaml
} }
foreach (var uri in GetUrisFor(type)) foreach (var uri in GetUrisFor(type))
{ {
Stream stream; if (assetLocator.Exists(uri))
try
{ {
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); throw new FileNotFoundException("Unable to find view for " + type.FullName);
} }

7
src/Perspex.Application/Platform/IAssetLoader.cs

@ -11,6 +11,13 @@ namespace Perspex.Platform
/// </summary> /// </summary>
public interface IAssetLoader 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> /// <summary>
/// Opens the resource with the requested URI. /// Opens the resource with the requested URI.
/// </summary> /// </summary>

19
src/Shared/PlatformSupport/AssetLoader.cs

@ -3,11 +3,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Resources;
using Perspex.Platform; using Perspex.Platform;
namespace Perspex.Shared.PlatformSupport namespace Perspex.Shared.PlatformSupport
@ -20,7 +18,6 @@ namespace Perspex.Shared.PlatformSupport
private static readonly Dictionary<string, Assembly> AssemblyNameCache private static readonly Dictionary<string, Assembly> AssemblyNameCache
= new Dictionary<string, Assembly>(); = new Dictionary<string, Assembly>();
static Assembly GetAssembly(string name) static Assembly GetAssembly(string name)
{ {
Assembly rv; Assembly rv;
@ -31,6 +28,20 @@ namespace Perspex.Shared.PlatformSupport
return rv; 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> /// <summary>
/// Opens the resource with the requested URI. /// Opens the resource with the requested URI.
/// </summary> /// </summary>
@ -46,7 +57,7 @@ namespace Perspex.Shared.PlatformSupport
var typeName = parts[parts.Length == 1 ? 0 : 1]; var typeName = parts[parts.Length == 1 ? 0 : 1];
var rv = asm.GetManifestResourceStream(typeName); var rv = asm.GetManifestResourceStream(typeName);
if (rv == null) if (rv == null)
throw new FileNotFoundException(uri.ToString()); throw new FileNotFoundException($"The resource {uri} could not be found.");
return rv; return rv;
} }
} }

Loading…
Cancel
Save