在LinqPad中创建WPF示例

时间:2011-03-18 20:37:44

标签: wpf linqpad

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

有关如何使其发挥作用的任何线索?

2 个答案:

答案 0 :(得分: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.

答案 1 :(得分:6)

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