【问题标题】:Creating WPF examples in LinqPad在 LinqPad 中创建 WPF 示例
【发布时间】:2011-03-18 20:37:44
【问题描述】:

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

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

【问题讨论】:

  • 更新:这已在最近的 LINQPad 测试版中得到修复 - 您可以以任何您想要的方式显示 WPF 窗口,并且没有可怕的死亡 :) 您还可以执行以下操作,在输出面板:PanelManager.DisplayWpfElement (new TextBlock() { Text = "Foo" });

标签: wpf linqpad


【解决方案1】:

另一种方法如下:

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.

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-09-27
      • 2011-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多