添加/启用adorner后如何刷新/更新wpf窗口

时间:2013-07-18 13:34:34

标签: c# wpf mvvm refresh adorner

我在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属性绑定到装饰器并向其发出通知。但是在此代码中,当方法终止时,装饰器会被画一段时间并被删除。

如何在UI被线程被阻止之前呈现它?

非常感谢任何帮助。

说明: 我喜欢使用装饰器在主选项卡上创建一个不可点击的半透明窗格,上面有一个文本,如“加载模块”。当我的MainThread使用magellan导航,解决与castle的依赖关系然后创建一个DevExpress控件时,我想显示此窗格。然后我再次删除它。我可以创建装饰,这没问题。装饰工作在我的原型项目中,我不做任何其他事情。

2 个答案:

答案 0 :(得分:1)

我找到了答案的解决方案。它可能不是超干净的方式,但它适用于我。

视图模型:

    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

答案 1 :(得分:0)

试试这个。

    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 {
    }

}