Browse Source

Try to do simple XAML loading.

Doesn't yet work - seems x:Class isn't supported by OmniXaml.
pull/81/head
Steven Kirk 11 years ago
parent
commit
279f9abab0
  1. 8
      samples/XamlTestApplication/Program.cs
  2. 8
      samples/XamlTestApplication/Views/MainWindow.cs
  3. 5
      samples/XamlTestApplication/Views/MainWindow.xaml
  4. 2
      src/Markup/Perspex.Xaml.Desktop/PerspexInflatableTypeFactory.cs
  5. 56
      src/Markup/Perspex.Xaml.Desktop/PerspexXamlLoader.cs

8
samples/XamlTestApplication/Program.cs

@ -17,6 +17,7 @@ namespace XamlTestApplication
using Perspex.Input;
using Perspex.Xaml.Desktop;
using ReactiveUI;
using Views;
class Item
{
@ -55,12 +56,7 @@ namespace XamlTestApplication
var testCommand = ReactiveCommand.Create();
testCommand.Subscribe(_ => Debug.WriteLine("Test command executed."));
var typeFactory = new PerspexInflatableTypeFactory();
var viewFactory = new ViewFactory(typeFactory);
viewFactory.RegisterViews(ViewRegistration.FromTypes(Assemblies.AssembliesInAppFolder.AllExportedTypes()));
var window = (Window) viewFactory.GetWindow("Main");
var window = new MainWindow();
window.Show();
Application.Current.Run(window);
}

8
samples/XamlTestApplication/Views/MainWindow.cs

@ -9,7 +9,15 @@
{
public MainWindow()
{
this.InitializeComponent();
DevTools.Attach(this);
}
private void InitializeComponent()
{
var loader = new PerspexXamlLoader(new PerspexInflatableTypeFactory());
loader.Load(this.GetType());
}
}
}

5
samples/XamlTestApplication/Views/MainWindow.xaml

@ -1,4 +1,7 @@
<Window Title="Perspex Test Application" Height="350" Width="525" xmlns="https://github.com/grokys/Perspex">
<Window x:Class="XamlTestApplication.MainWindow"
xmlns="https://github.com/grokys/Perspex"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Perspex Test Application" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />

2
src/Markup/Perspex.Xaml.Desktop/PerspexInflatableTypeFactory.cs

@ -9,7 +9,7 @@ namespace Perspex.Xaml.Desktop
public class PerspexInflatableTypeFactory : InflatableTypeFactory
{
public PerspexInflatableTypeFactory() : base(new TypeFactory(), new InflatableResourceTranslator(), typeFactory => new PerspexXamlLoader(typeFactory))
public PerspexInflatableTypeFactory() : base(new TypeFactory(), new InflatableTranslator(), typeFactory => new PerspexXamlLoader(typeFactory))
{
Inflatables = new Collection<Type> { typeof(Window), typeof(UserControl) };
}

56
src/Markup/Perspex.Xaml.Desktop/PerspexXamlLoader.cs

@ -1,11 +1,65 @@
namespace Perspex.Xaml.Desktop
{
using System;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Resources;
using OmniXaml;
public class PerspexXamlLoader : XamlLoader
{
public PerspexXamlLoader(ITypeFactory typeFactory) : base(new PerspexParserFactory(typeFactory))
public PerspexXamlLoader()
: this(new PerspexInflatableTypeFactory())
{
}
public PerspexXamlLoader(ITypeFactory typeFactory)
: base(new PerspexParserFactory(typeFactory))
{
}
public void Load(Type type)
{
this.Load(GetUriFor(type));
}
public void Load(string path)
{
var assembly = Assembly.GetEntryAssembly();
var resourceName = assembly.GetName().Name + ".g";
var manager = new ResourceManager(resourceName, assembly);
using (ResourceSet resourceSet = manager.GetResourceSet(CultureInfo.CurrentCulture, true, true))
{
var s = (Stream)resourceSet.GetObject(path, true);
if (s == null)
{
throw new IOException($"The requested resource could not be found: {path}");
}
this.Load(s);
}
}
private static string GetUriFor(Type type)
{
if (type.Namespace != null)
{
var toRemove = type.Assembly.GetName().Name;
var substracted = toRemove.Length < type.Namespace.Length ? type.Namespace.Remove(0, toRemove.Length + 1) : "";
var replace = substracted.Replace('.', Path.PathSeparator);
if (replace != string.Empty)
{
replace = replace + "/";
}
return replace + type.Name + ".xaml";
}
return null;
}
}
}
Loading…
Cancel
Save