我在使用 WPF 的 Adorner 时遇到了一些麻烦。我的装饰器没问题,但我没有在我想要的那一刻显示它。
我喜欢做类似的事情:
public void MyAction()
{
// property bound to the adorner VisibiltyProperty
// I like the happen a refresh now
// (code is wired correct, if I finish method here, the adorner is drawn)
this.IsAdornerEnabled = true;
try
{
... doing some long lasting things I cannot do async cause it depends on a lot of objects owned by main thread...
}
finally
{
// I like the adorner to be removed from UI now
this.IsAdornerEnabled = false;
}
}
IsAdornerEnabled 属性正确绑定(bind)到装饰器并向其发出通知。但是在这段代码中,当方法终止时,装饰器会在瞬间被绘制和移除。
如何在 UI 线程被阻塞之前渲染它?
非常感谢任何帮助。
解释:
我喜欢使用装饰器在我的主选项卡上创建一个不可点击的半透明 Pane ,上面带有“加载模块”之类的文本。当我的 MainThread 使用 magellan 导航,使用 CaSTLe 解决依赖关系,然后创建大量 DevExpress 控件时,我喜欢展示这个 Pane 。然后我再次删除它。我可以创建装饰器,这没问题。装饰器在我的原型(prototype)设计项目中工作,我不做任何其他事情。
最佳答案
我找到了答案的解决方案。这可能不是超干净的方式,但它对我有用。
View 模型:
private void LoadUi()
{
try
{
this.IsAdornerVisible = true;
RefreshCallback();
... some long going view initialization...
}
finally
{
this.IsAdornerVisible = false;
}
}
RefreshCallback 是 action 类型的属性,由 xaml 后面的代码中的方法设置。设置为 RefreshCallback 的方法是这样的:
private void Refresh()
{
this.Dispatcher.Invoke(new Action(() => { }), DispatcherPriority.ContextIdle, null);
}
使用 ContextIdle 在调度程序上调用使得渲染在执行空操作之前完成。 使用 ContextIdle 效果很好。在我尝试 DispatcherPriority 的其他值之前,例如 Render。不工作。现在我很高兴它可以工作,周末可以开始了。
我在这里找到的解决方案:
Source of my solution: Update the WPF UI now: how to wait for the rendering to finish
关于c# - 添加/启用装饰器后如何刷新/更新wpf窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17724975/