1

我在使用 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 控件时,我喜欢展示这个窗格。然后我再次删除它。我可以创建装饰器,这没问题。装饰器在我的原型设计项目中工作,我不做任何其他事情。

4

2 回答 2

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。不工作。现在我很高兴它可以工作,周末可以开始了。

我在这里找到的 解决方案:我的解决方案的来源:立即更新 WPF UI:如何等待渲染完成

于 2013-07-19T15:37:03.963 回答
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 {
    }

}
于 2013-07-18T14:54:07.617 回答