13

Is there any way to sanely instantiate WPF objects in LinqPad? Here's my example (the correct assemblies are added in the query, etc):

var w = new Window();
w.Loaded += (o,e) => {
    w.Content = new TextBlock() { Text = "Foo" };
};

w.Show();

However, this dies a horrible death:

System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used.

   at System.Windows.Input.TextServicesContext.StopTransitoryExtension()
   at System.Windows.Input.TextServicesContext.Uninitialize(Boolean appDomainShutdown)
   at System.Windows.Input.TextServicesContext.TextServicesContextShutDownListener.OnShutDown(Object target, Object sender, EventArgs e)
   at MS.Internal.ShutDownListener.HandleShutDown(Object sender, EventArgs e)

Any clues on how I can get this to work?

Ana Betts
  • 73,868
  • 16
  • 141
  • 209
  • 9
    Update: this is fixed in the recent LINQPad betas - you can show WPF windows any way you want and there are no horrible deaths :) You can also do the following, which displays a WPF element in the output panel: PanelManager.DisplayWpfElement (new TextBlock() { Text = "Foo" }); – Joe Albahari Mar 09 '12 at 13:01

2 Answers2

12

Another way to do it is as follows:

w.ShowDialog();
Dispatcher.CurrentDispatcher.InvokeShutdown();  // Cleanly end WPF session.

More examples:

new Window { Content = "Foo" }.ShowDialog();
new Window { Content = new Button { FontSize = 50, Content = "Foo" } }.ShowDialog();

Dispatcher.CurrentDispatcher.InvokeShutdown();  // Cleanly end WPF session.
Joe Albahari
  • 30,118
  • 7
  • 80
  • 91
8

You need to run a message loop by calling new Application().Run(w).

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964