diff --git a/Kaxaml.sln b/Kaxaml.sln index a1d9c65..7ec8163 100644 --- a/Kaxaml.sln +++ b/Kaxaml.sln @@ -1,6 +1,8 @@  -Microsoft Visual Studio Solution File, Format Version 11.00 -# Visual Studio 2010 +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.40629.0 +MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kaxaml", "Kaxaml\Kaxaml.csproj", "{FF5B3F61-822A-4154-9215-43D4A835EDF4}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KaxamlPlugins", "KaxamlPlugins\KaxamlPlugins.csproj", "{F6B1B201-8293-4729-842C-0BB54F89FFA7}" diff --git a/Kaxaml/DocumentViews/WpfDocumentView.xaml.cs b/Kaxaml/DocumentViews/WpfDocumentView.xaml.cs index b44c314..be1e683 100644 --- a/Kaxaml/DocumentViews/WpfDocumentView.xaml.cs +++ b/Kaxaml/DocumentViews/WpfDocumentView.xaml.cs @@ -400,6 +400,8 @@ private void Parse(bool IsExplicit) ContentArea.JournalOwnership = System.Windows.Navigation.JournalOwnership.UsesParentJournal; content = XamlReader.Load(ms, pc); + //content = XamlReader.Load(ms); + MainWindow.AddResources(content); } } diff --git a/Kaxaml/MainWindow.xaml.cs b/Kaxaml/MainWindow.xaml.cs index 61333bc..449f346 100644 --- a/Kaxaml/MainWindow.xaml.cs +++ b/Kaxaml/MainWindow.xaml.cs @@ -11,6 +11,10 @@ using KaxamlPlugins; using Microsoft.Win32; using PixelLab.Common; +using System.Windows.Markup; +using System.Reflection; +using System.Diagnostics; +using System.Collections.Generic; namespace Kaxaml { @@ -175,24 +179,141 @@ public MainWindow() // load or create startup documents - if (App.StartupArgs.Length > 0) + ParseArgs(App.StartupArgs); + + InitAssemblyResolve(); + + AddResources(App.Current); + + if (XamlDocuments.Count == 0) { - foreach (string s in App.StartupArgs) + WpfDocument doc = new WpfDocument(System.IO.Directory.GetCurrentDirectory()); + XamlDocuments.Add(doc); + } + + } + + private void ParseArgs(string[] args) + { + if (args == null) + return; + + for (int i = 0; i < args.Length; i++) + { + string arg = args[i]; + + if (arg == "-i") { - if (System.IO.File.Exists(s)) + // Handle the include command. This allows us to add dlls and static resources to the editor + string nextArg = (i < args.Length - 1) ? args[i + 1] : null; + if (nextArg != null) { - XamlDocument doc = XamlDocument.FromFile(s); - XamlDocuments.Add(doc); + try + { + if (nextArg.StartsWith("pack://")) + { + XamlResources.Add(nextArg); + } + else if (System.IO.File.Exists(nextArg)) + { + if (nextArg.EndsWith(".xaml", StringComparison.InvariantCultureIgnoreCase)) + { + XamlResources.Add(nextArg); + } + else if (nextArg.EndsWith(".dll", StringComparison.InvariantCultureIgnoreCase)) + { + Assembly.LoadFile(nextArg); + + string dir = System.IO.Path.GetDirectoryName(nextArg); + AssemblySearchDirs.Add(dir); + } + } + } + catch (Exception ex) + { + Console.WriteLine(ex); + Debug.Fail("Could not load: " + args + " " + nextArg); + } } + + i++; + continue; + } + + if (System.IO.File.Exists(arg)) + { + XamlDocument doc = XamlDocument.FromFile(arg); + XamlDocuments.Add(doc); } } + } - if (XamlDocuments.Count == 0) + private void InitAssemblyResolve() + { + if (AssemblySearchDirs.Count > 0) { - WpfDocument doc = new WpfDocument(System.IO.Directory.GetCurrentDirectory()); - XamlDocuments.Add(doc); + AppDomain currentDomain = AppDomain.CurrentDomain; + currentDomain.AssemblyResolve += AssemblyResolveEventHandler; + } + } + + private static Assembly AssemblyResolveEventHandler(object sender, ResolveEventArgs args) + { + string[] fields = args.Name.Split(','); + string assemblyName = fields[0]; + string assemblyCulture; + if (fields.Length < 2) + assemblyCulture = null; + else + assemblyCulture = fields[2].Substring(fields[2].IndexOf('=') + 1); + // Do the search + string assemblyFilePath = null; + foreach (string directory in AssemblySearchDirs) + { + string path = System.IO.Path.Combine(directory, assemblyName + ".dll"); + if (System.IO.File.Exists(path)) + { + assemblyFilePath = path; + break; + } } + // Load the assembly from the specified path + Assembly assembly = null; + if (!string.IsNullOrEmpty(assemblyFilePath)) + assembly = Assembly.LoadFrom(assemblyFilePath); + return assembly; + } + + private static readonly List AssemblySearchDirs = new List(); + private static readonly List XamlResources = new List(); + public static void AddResources(object element) + { + foreach (string filePath in XamlResources) + { + ResourceDictionary resDict = null; + if (filePath.StartsWith("pack://")) + { + resDict = new ResourceDictionary() { Source = new Uri(filePath) }; + } + else + { + resDict = XamlReader.Load(System.IO.File.Open(filePath, System.IO.FileMode.Open)) as ResourceDictionary; + } + if (resDict != null) + { + if (element is FrameworkElement) + { + FrameworkElement fe = element as FrameworkElement; + fe.Resources.MergedDictionaries.Add(resDict); + } + else if (element is Application) + { + Application app = element as Application; + app.Resources.MergedDictionaries.Add(resDict); + } + } + } } void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e) diff --git a/KaxamlPlugins/KaxamlInfo.cs b/KaxamlPlugins/KaxamlInfo.cs index bc425fb..ba8887b 100644 --- a/KaxamlPlugins/KaxamlInfo.cs +++ b/KaxamlPlugins/KaxamlInfo.cs @@ -40,7 +40,9 @@ public static IKaxamlInfoTextEditor Editor static void _Editor_TextSelectionChanged(object sender, RoutedEventArgs e) { - EditSelectionChanged(_Editor.SelectedText); + var handler = EditSelectionChanged; + if (handler != null) + handler(_Editor.SelectedText); } private static Window _MainWindow;