【问题标题】:Airspace Issue: Popup content over WindowsFormsHost will not be updated空域问题:WindowsFormsHost 上的弹出内容将不会更新
【发布时间】:2012-09-26 15:30:04
【问题描述】:

我遇到了空域问题。我的 WPF 应用程序托管了一个 winform 组件。我想在组件加载和长时间活动期间显示一个带有一些“等待”文本的弹出窗口。在这里我得到了我的问题:弹出窗口正确显示,但是当我处理组件的繁忙事件时,我无法更新弹出窗口内容。这里有一些 XAML 代码:

<WindowsFormsHost Name="wfh" Grid.Row="1" Grid.Column="0" ></WindowsFormsHost>
<Popup x:Name="Overlay" AllowsTransparency="True" Placement="Center" 
       StaysOpen="True"
       IsOpen="True" 
       PlacementTarget="{Binding RelativeSource={RelativeSource FindAncestor,   AncestorType=Grid, AncestorLevel=1}}">
    <TextBlock Name="tbWait"  Foreground="Red" />
</Popup>

和c#:

myWinformComponent.Event => (s, e) => 
{ 
   tbWait.Text = e.IsBusy ?  "Loading..." : string.Empty;
}

我知道 WinForm 和 WPF 的空域问题是什么,但我应该让弹出窗口始终打开,让我在 windowformshost 上显示任何内容。

编辑:我在后面的代码中放置了一些断点,我看到 Text 属性更改正确。此更改不会显示在 UI 中。

您有任何解决方法或解决方案吗? 谢谢各位!

【问题讨论】:

  • @HansPassant 我将尝试使用您链接的问题中报告的解决方案。但我担心我的问题不是 UI 冻结或类似问题,我可以使用 Dispatcher 或 BackgroundWorker 解决。
  • @HansPassant 您链接的答案有效!谢谢!

标签: c# wpf


【解决方案1】:

根据@HansPassant 的评论,我可以用这些行强制重绘:

DispatcherFrame frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(delegate(object parameter) {
        frame.Continue = false;
        return null;
    }), null);
Dispatcher.PushFrame(frame);

也许这不是最优雅的解决方案,但它是解决我的问题的有效补丁。

【讨论】:

    【解决方案2】:

    很遗憾,您无法在 WindowsFormsHost 上显示任何 WPF 内容。我自己花了很长时间与这个怪癖作斗争,直到我最终放弃并重新调整 UI 以使叠加层不必位于 WinForms 组件之上。

    【讨论】:

    • 我可以在我的 windowformshost 上显示弹出窗口:我的问题是我无法更新弹出窗口上的信息,尽管在代码隐藏中正确更改了属性。
    • 您是否真的看到了弹出窗口,或者您只是根据工具知道它的存在?
    • 嗯,这很令人惊讶。 :) 您是否尝试过删除 WFH 以查看弹出窗口是否在没有它的情况下正确更新?
    • 是的,如果我只是隐藏 WFH,弹出窗口将正确更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-07
    相关资源
    最近更新 更多