From e858d34b8faf70136691fc6192a1da792ad2b73f Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 28 Aug 2015 14:02:51 +0200 Subject: [PATCH] Simplify basic XAML load syntax. --- .../XamlTestApplication/Views/MainWindow.cs | 3 +- src/Markup/Perspex.Xaml/PerspexXamlLoader.cs | 33 ++++++++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/samples/XamlTestApplication/Views/MainWindow.cs b/samples/XamlTestApplication/Views/MainWindow.cs index 2f0dc53518..5280cca0c3 100644 --- a/samples/XamlTestApplication/Views/MainWindow.cs +++ b/samples/XamlTestApplication/Views/MainWindow.cs @@ -21,8 +21,7 @@ private void InitializeComponent() { - var xamlLoader = new PerspexXamlLoader(); - xamlLoader.Load(new Uri("Views/MainWindow.xaml", UriKind.Relative), this); + PerspexXamlLoader.Load(this); } } } \ No newline at end of file diff --git a/src/Markup/Perspex.Xaml/PerspexXamlLoader.cs b/src/Markup/Perspex.Xaml/PerspexXamlLoader.cs index b6bf6e44fa..297e40bf05 100644 --- a/src/Markup/Perspex.Xaml/PerspexXamlLoader.cs +++ b/src/Markup/Perspex.Xaml/PerspexXamlLoader.cs @@ -7,9 +7,10 @@ namespace Perspex.Xaml { using System; + using System.Reflection; using OmniXaml; - using Platform; using Perspex.Xaml.Context; + using Platform; using Splat; public class PerspexXamlLoader : XamlLoader @@ -24,6 +25,17 @@ namespace Perspex.Xaml { } + public static void Load(object obj) + { + var loader = new PerspexXamlLoader(); + loader.Load(obj.GetType(), obj); + } + + public object Load(Type type, object rootInstance = null) + { + return this.Load(GetUriFor(type), rootInstance); + } + public object Load(Uri uri, object rootInstance = null) { var assetLocator = Locator.Current.GetService(); @@ -39,5 +51,24 @@ namespace Perspex.Xaml return this.Load(stream, rootInstance); } } + + private static Uri GetUriFor(Type type) + { + if (type.Namespace != null) + { + var toRemove = type.GetTypeInfo().Assembly.GetName().Name; + var substracted = toRemove.Length < type.Namespace.Length ? type.Namespace.Remove(0, toRemove.Length + 1) : ""; + var replace = substracted.Replace('.', '/'); + + if (replace != string.Empty) + { + replace = replace + "/"; + } + + return new Uri(replace + type.Name + ".xaml", UriKind.Relative); + } + + return null; + } } }