Browse Source

Refactored loading assets into IAssetLoader.

pull/83/head
Steven Kirk 11 years ago
parent
commit
56b88d287c
  1. 11
      samples/XamlTestApplication/Program.cs
  2. 27
      samples/XamlTestApplication/Views/MainWindow.cs
  3. 7
      src/Markup/Perspex.Xaml/Context/PerspexParserFactory.cs
  4. 7
      src/Markup/Perspex.Xaml/Perspex.Xaml.csproj
  5. 43
      src/Markup/Perspex.Xaml/PerspexXamlLoader.cs
  6. 1
      src/Perspex.Application/Perspex.Application.csproj
  7. 27
      src/Perspex.Application/Platform/IAssetLoader.cs
  8. 48
      src/Windows/Perspex.Win32/AssetLoader.cs
  9. 5
      src/Windows/Perspex.Win32/Perspex.Win32.csproj
  10. 2
      src/Windows/Perspex.Win32/Win32Platform.cs

11
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;

27
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);
}
}
}

7
src/Markup/Perspex.Xaml/PerspexParserFactory.cs → 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);

7
src/Markup/Perspex.Xaml/Perspex.Xaml.csproj

@ -47,7 +47,7 @@
<Compile Include="DataBinding\ChangeTracking\PropertyMountPoint.cs" />
<Compile Include="DataBinding\ChangeTracking\PropertyPath.cs" />
<Compile Include="DataBinding\ChangeTracking\TargettedProperty.cs" />
<Compile Include="PerspexParserFactory.cs" />
<Compile Include="Context\PerspexParserFactory.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="DataBinding\DataContextChangeSynchronizer.cs" />
<Compile Include="DataBinding\IPerspexPropertyBinder.cs" />
@ -66,6 +66,7 @@
<Compile Include="Context\PerspexXamlMemberValuePlugin.cs" />
<Compile Include="Context\PerspexXamlType.cs" />
<Compile Include="Templates\XamlDataTemplate.cs" />
<Compile Include="PerspexXamlLoader.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
@ -76,6 +77,10 @@
<Project>{d211e587-d8bc-45b9-95a4-f297c8fa5200}</Project>
<Name>Perspex.Animation</Name>
</ProjectReference>
<ProjectReference Include="..\..\Perspex.Application\Perspex.Application.csproj">
<Project>{799a7bb5-3c2c-48b6-85a7-406a12c420da}</Project>
<Name>Perspex.Application</Name>
</ProjectReference>
<ProjectReference Include="..\..\Perspex.Base\Perspex.Base.csproj">
<Project>{B09B78D8-9B26-48B0-9149-D64A2F120F3F}</Project>
<Name>Perspex.Base</Name>

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

@ -0,0 +1,43 @@
// -----------------------------------------------------------------------
// <copyright file="PerspexXamlLoader.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
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<IAssetLoader>();
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);
}
}
}
}

1
src/Perspex.Application/Perspex.Application.csproj

@ -72,6 +72,7 @@
<Link>Properties\SharedAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Application.cs" />
<Compile Include="Platform\IAssetLoader.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>

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

@ -0,0 +1,27 @@
// -----------------------------------------------------------------------
// <copyright file="IAssetLoader.cs" company="Steven Kirk">
// Copyright 2015 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Platform
{
using System;
using System.IO;
/// <summary>
/// Loads assets compiled into the application binary.
/// </summary>
public interface IAssetLoader
{
/// <summary>
/// Opens the resource with the requested URI.
/// </summary>
/// <param name="uri">The URI.</param>
/// <returns>A stream containing the resource contents.</returns>
/// <exception cref="FileNotFoundException">
/// The resource was not found.
/// </exception>
Stream Open(Uri uri);
}
}

48
src/Windows/Perspex.Win32/AssetLoader.cs

@ -0,0 +1,48 @@
// -----------------------------------------------------------------------
// <copyright file="AssetLoader.cs" company="Steven Kirk">
// Copyright 2015 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Win32
{
using System;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Resources;
using Perspex.Platform;
/// <summary>
/// Loads assets compiled into the application binary.
/// </summary>
public class AssetLoader : IAssetLoader
{
/// <summary>
/// Opens the resource with the requested URI.
/// </summary>
/// <param name="uri">The URI.</param>
/// <returns>A stream containing the resource contents.</returns>
/// <exception cref="FileNotFoundException">
/// The resource was not found.
/// </exception>
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;
}
}
}
}

5
src/Windows/Perspex.Win32/Perspex.Win32.csproj

@ -63,6 +63,7 @@
<Compile Include="..\..\Shared\SharedAssemblyInfo.cs">
<Link>Properties\SharedAssemblyInfo.cs</Link>
</Compile>
<Compile Include="AssetLoader.cs" />
<Compile Include="Input\KeyInterop.cs" />
<Compile Include="Input\WindowsKeyboardDevice.cs" />
<Compile Include="Input\WindowsMouseDevice.cs" />
@ -81,6 +82,10 @@
<Project>{d211e587-d8bc-45b9-95a4-f297c8fa5200}</Project>
<Name>Perspex.Animation</Name>
</ProjectReference>
<ProjectReference Include="..\..\Perspex.Application\Perspex.Application.csproj">
<Project>{799a7bb5-3c2c-48b6-85a7-406a12c420da}</Project>
<Name>Perspex.Application</Name>
</ProjectReference>
<ProjectReference Include="..\..\Perspex.Base\Perspex.Base.csproj">
<Project>{B09B78D8-9B26-48B0-9149-D64A2F120F3F}</Project>
<Name>Perspex.Base</Name>

2
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()

Loading…
Cancel
Save