title: Hosting XNA inside WPF - Weird "Phantom" Window issue
body: <p>I'm trying to host an XNA game inside a WPF window using the Windows Forms host control. I've got a weird problem that a "Phantom" window is created when I run the game. It is created exactly at the first call to game's <code>Update</code> method exits.</p>
  
  <p>Here is the Update code, the default one from a new XNA project:</p>
  
  <pre><code>    protected override void Update(GameTime gameTime)
      {
          // Allows the game to exit
          if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
              this.Exit();
  
          // TODO: Add your update logic here
  
          base.Update(gameTime);
      }
  </code></pre>
  
  <p>The window is created <strong>after</strong> I step from the last curly brace at the very bottom. (yes, weird to have it NOT in <code>base.Update</code> but the after the <code>}</code> after it.)</p>
  
  <p>I have a Windows Forms host as I've said, with the code below:</p>
  
  <p>The relevant XAML is here (I've obviously got the Forms namespace etc. set up so no need to paste here):</p>
  
  <pre><code>&lt;WindowsFormsHost Margin="12" Name="windowsFormsHost1"&gt;
      &lt;forms:Panel x:Name="p"&gt;&lt;/forms:Panel&gt;
  &lt;/WindowsFormsHost&gt;
  </code></pre>
  
  <p>and in codebehind:</p>
  
  <pre><code>    private void Window_Loaded(object sender, RoutedEventArgs e)
      {
          Viewer v = new Viewer(p.Handle);
          v.Run();
      }
  </code></pre>
  
  <p>where the Viewer is my Game class (from XNA), with:</p>
  
  <pre><code>    public Viewer(IntPtr handle)
      {
          graphics = new GraphicsDeviceManager(this);
          Content.RootDirectory = "Content";
          panelHandle = handle;
          graphics.PreparingDeviceSettings += new EventHandler&lt;PreparingDeviceSettingsEventArgs&gt;(graphics_PreparingDeviceSettings);
      }
  
      void graphics_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
      {
          e.GraphicsDeviceInformation.PresentationParameters.DeviceWindowHandle = panelHandle;
      }
  </code></pre>
  
  <p>This technique is from a blog post I've found a few days ago (I can't find it again now, it is EXACTLY the same way -- with the exception that the post was old and doesn't have the issue I'm having).</p>
  
  <p>The problem is that, I build and run, but along with my "normal" window, there is a phantom window created. In my main window, XNA renders correctly. But the phantom window (the Window title is my assembly's name), doesn't have anything in it, is not resizable, cursor doesn't render inside it, and it acts like the main window in the sense that when I minimize this window, my main window stops rendering the XNA content until I un-minimize that phantom window. My program doesn't quit until I close BOTH windows. (If I close the phantom only, my main window, as you guess, stops rendering my XNA content). I tried iterating over my application windows, with 'App.Current.Windows', and all I've got is my main window listed there, that's why I'm calling that semi-responsive window the "phantom" window. It is not visible in my object model in WPF.</p>
  
  <p>When I wrote "XNA WPF phantom window", the first I've got was this: <a href="https://stackoverflow.com/questions/6673026/wpf-how-to-determine-the-origin-of-a-phantom-window">WPF: How to determine the origin of a phantom window?</a></p>
  
  <p>So I went and tried the Snoop. But Snoop probably also relies on the <code>Windows</code> iteration, and it also doesn't see any extra window there. I use the crosshair-drag feature (you drag the cursor around the screen and Snoop tells which process and window it belongs to), and Snoop tells that the Phantom Window is actually my MainWindow. But my MainWindow is ALSO the same window, according to Snoop. So this phantom is somewhat closely related (or maybe a "child"?) of my MainWindow instance, but I need a way to close it (or at least, hide it).</p>
  
  <p>Any ideas?</p>