简体   繁体   中英

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.
  • However, if I toggle this same property in Snoop, the windows stays on top.

My question is this: what is Snoop doing to force the window refresh?

在此处输入图片说明

What I have tried

I have tried the following:

  • window.UpdateLayout() ;
  • window.InvalidateVisual() ;
  • Adding a background task to continuously set this property.
  • Setting TopMost to false , then true , to trigger a DependencyProperty refresh.

It turns out that Snoop was not doing anything special.

If I wait an additional 500 milliseconds, then it works:

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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