/// <summary>
 /// Either create and show, or else re-attach to (if it's already up) the virtual keyboard.
 /// To be called from the desired target-Window that wants to use it.
 /// </summary>
 /// <param name="targetWindow">The Window that wants to be the owner of the virtual-keyboard Window and the target of it's output</param>
 public static void ShowOrAttachTo(IVirtualKeyboardInjectable targetWindow)
 {
     try
     {
         _desiredTargetWindow = targetWindow;
         // Evidently, for modal Windows I can't share user-focus with another Window unless I first close and then recreate it.
         // A shame. Seems like a waste of time. But I don't know of a work-around to it (yet).
         if (IsUp)
         {
             Console.WriteLine("VirtualKeyboard: re-attaching to a different Window.");
             VirtualKeyboard.The.Closed += new EventHandler(OnTheKeyboardClosed);
             VirtualKeyboard.The.Close();
             targetWindow.TheVirtualKeyboard = null;
         }
         else
         {
             VirtualKeyboard virtualKeyboard = ShowIt(targetWindow);
             targetWindow.TheVirtualKeyboard = virtualKeyboard;
         }
     }
     catch (Exception x)
     {
         Console.WriteLine("in VirtualKeyboard.ShowOrAttachTo: " + x.Message);
         IInterlocution inter = Application.Current as IInterlocution;
         if (inter != null)
         {
             inter.NotifyUserOfError("Well, now this is embarrassing.", "in VirtualKeyboard.ShowOrAttachTo.", x);
         }
     }
 }
 /// <summary>
 /// Create (and return) the VirtualKeyboard, and Show it.
 /// This is the factory-method for instantiating and showing the virtual keyboard.
 /// </summary>
 /// <param name="parentWindow">The window that will accept input from the keyboard</param>
 /// <returns>a reference to the VirtualKeyboard</returns>
 private static VirtualKeyboard ShowIt(IVirtualKeyboardInjectable targetWindow)
 {
     if (_theVirtualKeyboard == null)
     {
         _theVirtualKeyboard = new VirtualKeyboard();
     }
     _theVirtualKeyboard.Owner         = (Window)targetWindow;
     _theVirtualKeyboard.ShowActivated = false;
     if (!_theVirtualKeyboard.IsLoaded)
     {
         _theVirtualKeyboard.Show();
     }
     _theVirtualKeyboard.TargetWindow = targetWindow;
     if (targetWindow.ControlToInjectInto != null)
     {
         targetWindow.ControlToInjectInto.Focus();
     }
     return(_theVirtualKeyboard);
 }
 /// <summary>
 /// Handle the static Closed event of the virtual keyboard - for when I close it in order to re-open it for a different dialog-window
 /// (necessary for modal dialogs).
 /// </summary>
 private static void OnTheKeyboardClosed(object sender, EventArgs e)
 {
     _theVirtualKeyboard = ShowIt(_desiredTargetWindow);
 }
 /// <summary>
 /// Create (and return) the VirtualKeyboard, and Show it.
 /// This is the factory-method for instantiating and showing the virtual keyboard.
 /// </summary>
 /// <param name="parentWindow">The window that will accept input from the keyboard</param>
 /// <returns>a reference to the VirtualKeyboard</returns>
 private static VirtualKeyboard ShowIt(IVirtualKeyboardInjectable targetWindow)
 {
     if (_theVirtualKeyboard == null)
     {
         _theVirtualKeyboard = new VirtualKeyboard();
     }
     _theVirtualKeyboard.Owner = (Window)targetWindow;
     _theVirtualKeyboard.ShowActivated = false;
     if (!_theVirtualKeyboard.IsLoaded)
     {
         _theVirtualKeyboard.Show();
     }
     _theVirtualKeyboard.TargetWindow = targetWindow;
     if (targetWindow.ControlToInjectInto != null)
     {
         targetWindow.ControlToInjectInto.Focus();
     }
     return _theVirtualKeyboard;
 }
 /// <summary>
 /// Override the OnClosing method to cleanup and save any settings.
 /// </summary>
 protected override void OnClosing(CancelEventArgs e)
 {
     if (_viewModel != null)
     {
         _viewModel.Dispose();
     }
     #if !SILVERLIGHT
     //TODO ???
     //if (TargetWindow != null)
     //{
     //    // Remove any PreviewKeyDown event handlers..
     //    if (_allControlsHandlingPreviewKeyDownEventsFor != null)
     //    {
     //        foreach (var control in _allControlsHandlingPreviewKeyDownEventsFor)
     //        {
     //            if (control != null)
     //            {
     //                control.PreviewKeyDown -= new KeyEventHandler(OnPreviewKeyDown);
     //            }
     //        }
     //    }
     //}
     #endif
     // Remembered which language it was set to last time, so that we can pre-set it to that next time around.
     //TODO
     #if !SILVERLIGHT
     //if (_appSettings != null)
     //{
     //    if (_isKeyboardLayoutChanged)
     //    {
     //        //Console.WriteLine("  Writing that to setting VKLayout.");
     //        _appSettings["VKLayout"] = VirtualKeyboard.LastKeyboardLayoutWasSetTo.ToString();
     //        _appSettings.Save();
     //    }
     //}
     #endif
     // Set the static object to null so that we know that this has closed
     // and would need to be recreated the next time.
     VirtualKeyboard._theVirtualKeyboard = null;
 }