Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating WPF examples in LinqPad

Tags:

wpf

linqpad

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?

like image 354
Ana Betts Avatar asked Mar 18 '11 20:03

Ana Betts


1 Answers

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.
like image 69
Joe Albahari Avatar answered Oct 02 '22 03:10

Joe Albahari