internal static void Run(string markup, string script, object parent) { MarkupHost mh = new MarkupHost(); MarkupHost mhp = (MarkupHost)mh.NewPresentation(markup, script, parent); if (mhp != null) { Application app = new Application(); app.DispatcherUnhandledException += App_Exception; app.Run(mhp.hostedWindow); } }
public object NewPresentation(string markupFile, string scriptFile = null, object context = null) { try { MarkupHost host = null; using (StreamReader sr = File.OpenText(markupFile)) { var nsm = new XmlNamespaceManager(new NameTable()); nsm.AddNamespace(string.Empty, "http://schemas.microsoft.com/winfx/2006/xaml/presentation"); nsm.AddNamespace("x", "http://schemas.microsoft.com/winfx/2006/xaml"); var pc = new XmlParserContext(null, nsm, null, XmlSpace.None); Window win = (Window)XamlReader.Load(XmlReader.Create(sr, null, pc)); win.Owner = hostedWindow; if (win.Title == "") { win.Title = Path.GetFileNameWithoutExtension(markupFile); } if (scriptFile == null) { scriptFile = win.Tag as string; } if (scriptFile == null && File.Exists(markupFile + ".js")) { scriptFile = markupFile + ".js"; } if (scriptFile != null) { List <string> fields = new List <string>(); fields.Add("Parent"); AddElementField(win, fields); host = ActivateScript <MarkupHost>(scriptFile, fields); host.hostedWindow = win; host.parent = context; host.Load(); MethodInfo[] methods = host.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); foreach (MethodInfo mi in methods) { int u = mi.Name.IndexOf('_'); if (u >= 0 && mi.GetParameters().Length == 2) { string ctlName = mi.Name.Substring(0, u); string evtName = mi.Name.Substring(u + 1); object ctrl = win.FindName(ctlName); if (ctrl != null) { host.AttachHandler(ctrl, evtName, mi.Name); } } } } else { host = new MarkupHost(); host.hostedWindow = win; } } return(host); } catch (XamlParseException ex) { MessageBox.Show(markupFile + " - " + ex.Message, "Markup Error", MessageBoxButton.OK, MessageBoxImage.Error); } catch (ArgumentException ex) { MessageBox.Show(ex.Message, "Script Error", MessageBoxButton.OK, MessageBoxImage.Error); } catch (TargetInvocationException ex) { MessageBox.Show(ex.InnerException.Message, "Initialization Error", MessageBoxButton.OK, MessageBoxImage.Error); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Load Error", MessageBoxButton.OK, MessageBoxImage.Error); } return(null); }