等待UI重新呈现完成 [英] Waiting for UI re-rendering to complete

查看:66
本文介绍了等待UI重新呈现完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里实际上遇到了2个类似的问题,没有运气在网上找到任何东西.

I've actually got 2 similar problems here with no luck finding anything on the web.

问题1:使用BackgroundWorker,我正在完成%的情况下更新了UI,但是我正在使用UserState,因为我想报告百分比的百分比.问题是,根据输入的不同,有时很少会发生更新(每两秒钟一个百分比),而其他时候却非常快(每秒触发多次更新的小数百分比).在后一种情况下,我遇到了堆栈溢出(无双关语)的问题.我猜测ProgressChanged事件的调用太多了.现在,这是原型代码,我将直接在progressChanged事件中更新TextBlock,而不使用ViewModels,但稍后再讲.不知道这是否可能是问题.有没有一种方法可以让此更改进度的事件被调用,它需要多长时间运行一次,但是要说些类似的话:如果(!mytextblock.IsRendering())mytextblock.text = newPercent;

Problem 1: Using a BackgroundWorker, I am updating the UI with the % done, but I am using the UserState because I want to report fraction of a percent. The problem is, depending on the inputs, sometimes updates happen rarely (a percent every couple seconds) and other times very fast (triggering fractional % updates many times a second). In the latter case, I'm getting a stack overflow (no pun intended) issue. I'm guessing the ProgressChanged event is just getting called too much. This is for now prototype code and I'm updating a TextBlock directly in the progressChanged event and not using ViewModels, but I will later. Not sure if this might be the problem. Is there a way to allow this progress changed event to get called how often it needs to, but say something like: if (!mytextblock.IsRendering()) mytextblock.text = newPercent;

这样,它会在绘制完最后一个数字后立即进行更新.如果跳过百分比,那没关系.

That way it updates just when it's done drawing the last number. If percents get skipped, that's ok.

问题2:这是一个个人项目,在这里我进行屏幕截图,以某种方式对其进行更改,然后在wpf程序中显示更改后的图像,并不断重复.有没有办法说:抓屏编辑图片更新UIWaitForUIToRender//< -------我该怎么做?重复

Problem 2: This is a personal project where I'm taking a screen capture, changing it in some way, then showing the changed image within the wpf program, and repeat continuously. Is there a way to say: GrabScreen EditImage UpdateUI WaitForUIToRender // <------- how do I do this? Repeat

谢谢

推荐答案

 public class TimedAction 
{
    public static void ExecuteWithDelay(Action action, TimeSpan delay)
    {
        var timer = new DispatcherTimer();
        timer.Interval = delay;
        timer.Tag = action;
        timer.Tick += timer_Tick;
        timer.Start();
    }

    static void timer_Tick(object sender, EventArgs e)
    {
        var timer = (DispatcherTimer)sender;
        var action = (Action)timer.Tag;

        action.Invoke();
        timer.Stop();
    }

    public static void ExecuteAfterRendering(Action action)
    {
        ExecuteAfterFrames(action, 3);
    }

    public static void ExecuteAfterFrames(Action action, int frames)
    {
        var timedAction = new TimedAction();
        timedAction._currentAction = action;
        timedAction._framesToWait = frames;
    }

    private Action _currentAction;
    private int _framesToWait;
    private int _currentFrame = 0;
    private TimedAction()
    {
        CompositionTarget.Rendering += CompositionTarget_Rendering;
    }

    private void Dispose()
    {
        CompositionTarget.Rendering -= CompositionTarget_Rendering;
    }

    void CompositionTarget_Rendering(object sender, EventArgs e)
    {
        _currentFrame++;
        if (_currentFrame == _framesToWait)
        {
            _currentAction.Invoke();
            Dispose();
        }
    }

}

这篇关于等待UI重新呈现完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆