Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to reference other DLLs #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Kaxaml.sln
Original file line number Diff line number Diff line change
@@ -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}"
Expand Down
2 changes: 2 additions & 0 deletions Kaxaml/DocumentViews/WpfDocumentView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
137 changes: 129 additions & 8 deletions Kaxaml/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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<string> AssemblySearchDirs = new List<string>();
private static readonly List<string> XamlResources = new List<string>();

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)
Expand Down
4 changes: 3 additions & 1 deletion KaxamlPlugins/KaxamlInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down