简体   繁体   English

使用WPF,Snoop如何在属性更改时强制刷新窗口?

[英]With WPF, how does Snoop force a window refresh on a property change?

I have a very odd problem: 我有一个很奇怪的问题:

  • In my C# code, if I set the "Topmost" property, the window does not stay on top. 在我的C#代码中,如果我设置“ Topmost”属性,则窗口不会停留在顶部。
  • However, if I toggle this same property in Snoop, the windows stays on top. 但是,如果在Snoop中切换此相同属性,则窗口将停留在顶部。

My question is this: what is Snoop doing to force the window refresh? 我的问题是这样的:Snoop在做什么以强制刷新窗口?

在此处输入图片说明

What I have tried 我尝试过的

I have tried the following: 我尝试了以下方法:

  • window.UpdateLayout() ; window.UpdateLayout() ;
  • window.InvalidateVisual() ; window.InvalidateVisual() ;
  • Adding a background task to continuously set this property. 添加后台任务以连续设置此属性。
  • Setting TopMost to false , then true , to trigger a DependencyProperty refresh. TopMost设置为false ,然后设置为true ,以触发DependencyProperty刷新。

It turns out that Snoop was not doing anything special. 事实证明,史努比没有做任何特别的事情。

If I wait an additional 500 milliseconds, then it works: 如果我再等待500毫秒,则可以:

Task task = Task.Run(
    async () =>
    {
        // Without this delay, the change will not work.
        await Task.Delay(TimeSpan.FromMilliseconds(500));

        Application.Current.Dispatcher.Invoke(
            () =>
            {
                floatingPaneWindow.Topmost = true;
                {
                    if (window.Top < 0)
                    {
                        window.Top = 0;
                    }
                        if (window.Left < 0)
                    {
                        window.Left = 0;
                    }
                }                                               
            });
    });

Of course, adding specific times into an application is very hacky, so I'm currently looking for some method to determine if the window is properly set up. 当然,在应用程序中添加特定的时间是很棘手的,所以我目前正在寻找确定窗口是否正确设置的方法。

Update 更新资料

Puzzle solved, use the Dispatcher select an appropriate priority, see WPF: In an attached property, how to wait until visual tree loaded properly? 拼图解决后,使用Dispatcher选择适当的优先级,请参阅WPF:在附加属性中,如何等待直到视觉树正确加载?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM