[英]How to refresh / update the wpf window after adorner has been added / enabled
I am having some troubles with the Adorner of WPF. 我在WPF的崇拜者方面遇到了麻烦。 My adorner is ok, but I don't get it displayed at the very moment I want to.
我的装饰师还可以,但是在我想要的那一刻我没有显示它。
I like to do something like: 我喜欢做类似的事情:
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;
}
}
The IsAdornerEnabled Property is correct bound to the adorner and makes a notification to it. IsAdornerEnabled属性正确绑定到装饰器,并对其进行通知。 But in this code the adorner is painted and removed for a split second when the method terminates.
但是在此代码中,当方法终止时,装饰器被绘制并移除了一秒钟。
How is it possible to get it rendered before the UI is thread is blocked? 在阻止UI线程之前如何渲染它?
Any help is very appreciated. 任何帮助都非常感谢。
Explanation: I like to use the adorner to create a non-clickable, half transparent pane over my main tab with a text like "loading module" on it. 说明:我喜欢使用装饰器在主选项卡上创建一个不可单击的半透明窗格,上面带有诸如“正在加载模块”之类的文本。 While my MainThread is navigating with magellan, resolving dependencies with castle and then creating a lof of DevExpress controls, I like to show this pane.
当我的MainThread使用magellan导航,使用Castle解决依赖关系,然后创建一组DevExpress控件时,我喜欢显示此窗格。 Then I remove it again.
然后,我再次将其删除。 I can create the adorner, that's no problem.
我可以创建装饰器,没问题。 The adorner works in my prototyping project, where I don't do any other things.
装饰器在我的原型设计项目中工作,在此我不做任何其他事情。
I found the solution for my answer. 我找到了答案的解决方案。 It might no be the ultra clean way, but it works for me.
可能不是超级干净的方法,但是对我有用。
ViewModel: 视图模型:
private void LoadUi()
{
try
{
this.IsAdornerVisible = true;
RefreshCallback();
... some long going view initialization...
}
finally
{
this.IsAdornerVisible = false;
}
}
RefreshCallback is a property of type action, set by a method in code behind of the xaml. RefreshCallback是action类型的属性,由xaml后面代码中的方法设置。 The method set to the RefreshCallback is this one:
设置为RefreshCallback的方法是这样的:
private void Refresh()
{
this.Dispatcher.Invoke(new Action(() => { }), DispatcherPriority.ContextIdle, null);
}
The invoke on the dispatcher with ContextIdle makes that the rendering is done before the empty action is executed. 使用ContextIdle对调度程序的调用使渲染在执行空操作之前完成。 With ContextIdle this works good.
使用ContextIdle可以很好地工作。 Before I tried other values of DispatcherPriority, for example Render.
在尝试DispatcherPriority的其他值之前,例如Render。 Did not work.
不工作。 Now I'm happy it works and weekend can begin.
现在我很高兴它能正常工作,周末可以开始。
The solution I found here: Source of my solution: Update the WPF UI now: how to wait for the rendering to finish 我在这里找到的解决方案:解决方案的来源:立即更新WPF UI:如何等待渲染完成
Try this. 尝试这个。
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
{
this.Dispatcher.Invoke( (Action)(() =>
{
// Do your work here,
this.IsAdornerEnabled = false;
}));
}catch {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.