2011-03-18 31 views
12

有没有什么方法可以在LinqPad中正确实例化WPF对象?下面是我的示例(正确的组件在查询中添加等):在LinqPad中创建WPF示例

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

w.Show(); 

然而,这死一个可怕的死亡:

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) 

我如何能得到这个工作的任何线索?

+9

更新:这是固定在最近LINQPad测试版 - 您可以显示WPF窗口你想要的任何方式没有可怕的死亡:)你也可以执行以下操作,在输出面板中显示一个WPF元素:PanelManager.DisplayWpfElement(new TextBlock(){Text =“Foo”}); – 2012-03-09 13:01:39

回答

6

您需要通过调用new Application().Run(w)来运行消息循环。

+0

完美的工作!谢谢你的提示。 – 2011-03-18 21:08:00

10

另一种方式来做到这一点是如下:

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

更多的例子:

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

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