【问题标题】:WPF MVVM update status while processing处理时 WPF MVVM 更新状态
【发布时间】:2013-08-05 18:38:41
【问题描述】:

我正在使用 WPF MVVM 中的枚举状态发出的验证信号。通过单击按钮触发验证。 这是枚举和命令的代码:

public enum StatusTest {None, Ok, Error, Processing }

public ICommand TestConnectionCommand
{
    get
    {
        if (_testConnectionCommand == null)
            _testConnectionCommand = new RelayCommand(
                () => this.Test());

        return _testConnectionCommand;
    }
}
void Test()
{
    Status = StatusTest.Processing;
    if ( ValidationMethod()) Status = StatusTest.Ok;
    else Status = StatusTest.Error;
}

在按钮旁边我有一个圆圈,与枚举 StatusTest 相关联,它改变了您的填充状态更改。 目前它只显示最终状态(正常或错误),从不处理。如何在验证过程中让圆圈被颜色处理填充?

【问题讨论】:

  • 如果状态正在更新以显示正常或错误,那么很可能该过程刚刚完成得足够快以至于它不显示正在处理。您能否延迟验证方法,看看会发生什么?
  • 我延迟了 5 秒 System.Threading.Thread.Sleep

标签: wpf mvvm enums


【解决方案1】:

您的所有工作似乎都在 UI 线程上,因此第一个设置器状态不会生效。将您的代码更改为下面,让 Test() 在不同的线程上工作。

public enum StatusTest {None, Ok, Error, Processing }

public ICommand TestConnectionCommand
{
    get
    {
        if (_testConnectionCommand == null)
            _testConnectionCommand = new RelayCommand(
                () => ThreadPool.QueueUserWorkItem(Test));

        return _testConnectionCommand;
    }
}
void Test(object state)
{
    Status = StatusTest.Processing;
    if ( ValidationMethod()) Status = StatusTest.Ok;
    else Status = StatusTest.Error;
}

【讨论】:

    猜你喜欢
    • 2017-01-14
    • 1970-01-01
    • 2023-01-12
    • 2013-11-23
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    相关资源
    最近更新 更多