2011-03-18 12 views
12

¿Hay alguna manera de crear instancias sensatas de objetos WPF en LinqPad? Aquí está mi ejemplo (los conjuntos correctos se agregan en la consulta, etc.):Creando ejemplos de WPF en LinqPad

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

w.Show(); 

Sin embargo, este muere una muerte horrible:

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) 

Alguna pista sobre cómo puedo conseguir que esto funcione?

+9

Actualización: este se fija en los últimos betas LINQPad - se puede mostrar ventanas de WPF cualquier forma que desee y no hay muertes horribles :) También puede hacer lo siguiente, que muestra un elemento WPF en el panel de salida: PanelManager.DisplayWpfElement (new TextBlock() {Text = "Foo"}); –

Respuesta

6

Debe ejecutar un ciclo de mensajes llamando al new Application().Run(w).

+0

¡Eso funcionó perfectamente! Gracias por el consejo. –

10

Otra manera de hacerlo es la siguiente:

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

Más ejemplos:

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

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