12

有没有办法在 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)

关于如何让它工作的任何线索?

4

2 回答 2

11

另一种方法如下:

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.
于 2011-03-19T01:56:40.887 回答
7

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

于 2011-03-18T20:44:51.140 回答