本文介绍了在WPF中的延迟后复位变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有执行,并得到执行的返回值有些code。我这个值设置为我的窗口的依赖属性,因为有风格触发绑定它。当变量为0,它使用的默认样式,当1微红的风格,并在2绿。

I have some code that executes and gets the return value of execution.I set this value to a dependency property of my window because there are style triggers bound to it.When the variable is 0 it uses the default style, when 1 a reddish style, and when 2 a greenish.

但我有一些实用的方式经过一段时间重置这种风格。

But i have to reset this style after a while in some practical way.

什么是做到这一点的最简单的方法是什么?

What is the easiest method to do this?

if (!Compiler.TryCompile(strText, Models[Model], EntryPoint.Text, out error))
{
    output.Items.Add("Error compiling:");
    output.Items.Add(error);
    CompilationStatus = 1; // dependency property bound on ui
}
else {
    output.Items.Add("Compilation successful!");
    CompilationStatus = 2; // dependency property bound on ui
}

// should execute this after 5 seconds 
CompilationStatus = 0;  // dependency property bound on ui

WPF和.NET 4中使用的项目。谢谢!

WPF and .Net 4 is used in project.Thanks!!

推荐答案

我通常使用自定义的扩展方法如下:

I usually use a custom extension method for this:

public static class DispatcherHelper
{
    public static void DelayInvoke(this Dispatcher dispatcher, TimeSpan ts, Action action)
    {
        DispatcherTimer delayTimer = new DispatcherTimer(DispatcherPriority.Send, dispatcher);
        delayTimer.Interval = ts;
        delayTimer.Tick += (s, e) =>
        {
            delayTimer.Stop();
            action();
        };
        delayTimer.Start();
    }
}

在你的情况,你可以使用它是这样的:

In your case you can then use it like this:

Dispatcher.DelayInvoke(TimeSpan.FromSeconds(5), () => 
{
   CompilationStatus = 0; 
}

编辑:我已经忘记了,但看起来这种方法最初是在这个SO线程写的乔恩飞碟双向:延迟调度调用?

这篇关于在WPF中的延迟后复位变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 19:02