protected override void BeginProcessing() { _xamlUI = ((PoshConsole.Host.PoshOptions)Host.PrivateData.BaseObject).WpfConsole; _xamlUI.Dispatcher.Invoke((Action)(() => { _window = _xamlUI.RootWindow; Block b = _xamlUI.CurrentBlock; while (b.Tag == null && b.PreviousBlock != null) { b = b.PreviousBlock; } if (b.Tag is int) { _id = (int)b.Tag; _numbered = b; } })); base.BeginProcessing(); }
// private Huddled.WPF.Controls.Interfaces.IPSXamlConsole xamlUI; // private FlowDocument _document = null; #region Methods protected override void BeginProcessing() { if (_window == null) { _template = GetXaml(); if (Host.PrivateData != null && Host.PrivateData.BaseObject is IPSWpfOptions) { //((IPSWpfHost)).GetWpfConsole(); _xamlUI = ((IPSWpfOptions)Host.PrivateData.BaseObject).WpfConsole; if (Popup.ToBool()) { //_window = new Window //{ // WindowStyle = WindowStyle.ToolWindow, // // Content = XamlHelper.NewItemsControl() //}; //_xamlUI.PopoutWindows.Add(_window); //_window.Show(); //_dispatcher = _window.Dispatcher; ////////////////////////////////////////////////////////////// // If they ask for a popup from a IPSWpfOptions, we still use the Presentation // That way the threading "issues" are the same for both Wpf and non-Wpf hosts // Otherwise we'd have to deal with two different realities var result = Presentation.Start(_template, null, null); _window = result.Window; _dispatcher = result.Dispatcher; } else { //_window = _xamlUI.RootWindow; _dispatcher = _xamlUI.Dispatcher; _xamlUI.RootWindow.LoadTemplates(); } } else { var result = Presentation.Start(_template, null, null); _window = result.Window; _dispatcher = result.Dispatcher; } if (_template != null) { ErrorRecord error = (ErrorRecord)_dispatcher.Invoke((Func<ErrorRecord>)(() => { ErrorRecord err = null; _template.TryLoadXaml(out _element, out err); if (_element is Window) { if (Host.PrivateData != null && Host.PrivateData.BaseObject is IPSWpfOptions) { _window = (Window)_element; _window.Show(); _dispatcher = _window.Dispatcher; } _element = null; } return err; })); if (error != null) { WriteError(error); } } } // internal reference count _windowCount++; base.BeginProcessing(); }
protected override void EndProcessing() { // release only in OutWPFCommand //if (_windowCount == 0) //{ _window = null; _dispatcher = null; _xamlUI = null; _windowCount--; //} base.EndProcessing(); }
protected override void BeginProcessing() { _xamlUI = ((PoshConsole.Host.PoshOptions)Host.PrivateData.BaseObject).XamlUI; ErrorRecord error = (ErrorRecord)_xamlUI.Dispatcher.Invoke((Func<ErrorRecord>)(() => { ErrorRecord err = null; if (Popup.ToBool()) { _host = NewContainer(); _window = new Window { WindowStyle = WindowStyle.ToolWindow, Content = _host }; _xamlUI.PopoutWindows.Add(_window); _window.Show(); } #region templates switch (ParameterSetName) { case "FileTemplate": { string templates = Path.Combine(Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName), "XamlTemplates"); string template = null; if (!FileTemplate.Exists) { // try to magically resolve the file template = System.IO.Path.Combine(base.CurrentProviderLocation("FileSystem").Path, FileTemplate.Name); if (File.Exists(template)) { FileTemplate = new FileInfo(template); } else { template = Path.Combine(templates, FileTemplate.Name); if (File.Exists(template)) { FileTemplate = new FileInfo(template); } else { throw new FileNotFoundException("Can't find the template file. There is currently no default template location, so you must specify the path to the template file.", template); } } } FileTemplate.TryLoadXaml(out _element, out err); } break; case "SourceTemplate": { SourceTemplate.TryLoadXaml(out _element, out err); } break; } #endregion templates return err; })); if (error != null) { WriteError(error); } base.BeginProcessing(); }
/// <summary> /// Initializes a new instance of the <see cref="XamlConsole"/> class. /// </summary> /// <param name="console">The console.</param> public PoshWpfConsole(IPSWpfConsole console) { _console = console; }
protected override void BeginProcessing() { base.BeginProcessing(); TouchDefaults(); RedefineWriteOutput(); _wantedAsync = Async.ToBool(); if (_window == null) { _template = GetXaml(); if (!Owner.HasValue) { Owner = OwnerWindow != null ? new WindowInteropHelper(OwnerWindow).Handle : Process.GetCurrentProcess().MainWindowHandle; } // If they don't want a popup (or Async Threaded, which forces the issue) // AND if we're in a WPF host (so we can give them what they want)... if (Host.PrivateData != null && Host.PrivateData.BaseObject is IPSWpfOptions && Inline.ToBool() && !Async.ToBool()) { //((IPSWpfHost)).GetWpfConsole(); _xamlUI = ((IPSWpfOptions)Host.PrivateData.BaseObject).WpfConsole; //_window = _xamlUI.RootWindow; _dispatcher = _xamlUI.Dispatcher; _xamlUI.RootWindow.LoadTemplates(); } // if they *want* it shown async ... // Or it MUST be shown Async (because we're not in a STA thread) else if (_wantedAsync || System.Threading.Thread.CurrentThread.GetApartmentState() != System.Threading.ApartmentState.STA) { Async = true; Inline = false; _asyncResult = Presentation.Start(_template, SetWindowParams, Owner); _window = _asyncResult.Window; _dispatcher = _asyncResult.Dispatcher; } // otherwise, // they do not want the async thread, and we're in a STA host // so we'll just create the window and call ShowDialog() at the end... else { // Async = false; // this is already false Inline = false; _window = Presentation.NewWindow(_template, Owner); SetWindowParams(_window); _dispatcher = _window.Dispatcher; } // preload the template into _element ... if (_template != null) { ErrorRecord error = (ErrorRecord)_dispatcher.Invoke((Func<ErrorRecord>)(() => { ErrorRecord err = null; if (_template.TryLoadXaml(out _element, out err)) { // if the xaml template is a window, we will toss it unless we're in a WpfHost... if (_element is Window) { //if (Host.PrivateData != null && Host.PrivateData.BaseObject is IPSWpfOptions) //{ // if(_window != null) // { // ShowUIWindowDictionary.Instance.Remove(_window); // } // _window = (Window)_element; // //_window.Show(); // try // { // SetWindowParams(_window); // } // catch (Exception ex) // { // err = new ErrorRecord(ex, "Error Setting Parameter Property", ErrorCategory.InvalidArgument, Content); // } // _dispatcher = _window.Dispatcher; //} _element = null; } return err; } else { ContentElement conElement; if (_template.TryLoadXaml(out conElement, out err)) { DocumentOutput(conElement); } return err; } })); if (error != null) { WriteError(error); } } } if (_window != null) { _window.Closed += (object sender, EventArgs e) => { UIWindowDictionary.Instance.Remove((Window)sender); ((System.Collections.Hashtable)((PSObject)SessionState.PSVariable.GetValue("Global:ShowUI")).BaseObject)["ActiveWindow"] = null; }; } }
protected override void EndProcessing() { if(ExportNamedElements.ToBool()) { if (_window.Dispatcher != null && Dispatcher.CurrentDispatcher != _window.Dispatcher) { _window.Dispatcher.Invoke((Action)(() => _window.Loaded += (s, a) => ExportVisual(s as Visual, "global") )); } else { _window.Loaded += (s, a) => ExportVisual(s as Visual, "global"); } } try { //if (Host.PrivateData.BaseObject is IPSWpfOptions && !Popup.ToBool() && !Async.ToBool()) //else if (Async.ToBool() || System.Threading.Thread.CurrentThread.GetApartmentState() != System.Threading.ApartmentState.STA) if (!Async.ToBool() && !Inline.ToBool()) { _window.ShowDialog(); } // if they INTENDED it to be synchronous, simulate that for them :) if (!_wantedAsync) { if (_asyncResult != null) { _asyncResult.AsyncWaitHandle.WaitOne(); } WriteObject(GetVariableValue("Global:UIOutput", null), true); } } catch (Exception ex) { if (_window != null) { _window.Close(); } WriteError(new ErrorRecord(ex, "Error displaying WPF Content", ErrorCategory.NotSpecified, Content)); } finally { _window = null; _dispatcher = null; _xamlUI = null; } //UndefineWriteOutput(); base.EndProcessing(); }