Mar*_*mur 5 .net c# wpf user-interface visual-studio
目前我正在尝试调试一些代码,其中我正在检查项目的可见性(.IsVisible()例如使用方法)。问题是,当我从一个断点跳到下一个断点或在行之间跳转时,数据显然发生了变化,但程序的 UI 似乎根本没有变化。这让我很难判断事物是否可见,我必须信任 Visual Studio。
有没有办法在调试时更新 UI,这样我也可以看到那里的变化?
您必须强制同步重新渲染 UI。您可以在某处定义此扩展方法:
public static void SynchronouslyRedraw(this UIElement uiElement) {
uiElement.InvalidateVisual();
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() => { })).Wait();
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() => { })).Wait();
}
Run Code Online (Sandbox Code Playgroud)
并在每个断点之后调用它Window(使用立即窗口、断点操作、附加代码行等)。它应该同步重新渲染Window有问题的:
this.SynchronouslyRedraw(); // assuming your breakpoint is in your `Window` class for example.
Run Code Online (Sandbox Code Playgroud)
请注意,此方法适用于UIElement对象可视树中的任何对象Window。