gpt4 book ai didi

c# - 确定的日期时间还剩 4 分钟时启用按钮

转载 作者:行者123 更新时间:2023-11-30 18:22:05 28 4
gpt4 key购买 nike

我正在使用 MvvmLight 开发 WPF 应用程序。我想在结算时间还剩 4 分钟时启用按钮。它应该在 UI 中自动启用。我应该使用线程来持续跟踪 CurrentUTCtime 和 mytime 的差异吗?如果是,那么如何在这段代码中使用线程呢?这是我的代码。

Datetime mytime = new DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,22,0,0)

public RelayCommand Open
{
get
{
return Open?? (_open= new RelayCommand(ExecuteOpen, CanExecuteOpen));
}

private void ExecuteOpen()
{
_navigation.NavigationToSetBetsDialogue();
}

private bool CanExecuteOpen()
{
double? remainingMinutes = null;

DateTime CurrentUTCtime = DateTime.UtcNow;
remainingMinutes = mytime .Subtract(CurrentUTCtime).TotalMinutes;

if (remainingMinutes <= 4)
{
return true;
}
else
{
return false;
}
}
}

最佳答案

尝试使用 DispatcherTimer:

        private DispatcherTimer _Timer = new DispatcherTimer();
private bool _CanExecute;

private void StartTimer()
{
var mytime = new DateTime( DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 22, 0, 0 );

var interval = DateTime.UtcNow - mytime - TimeSpan.FromMinutes( 4 );
_Timer.Interval = interval;
_Timer.Tick += Timer_Tick;
}

private void Timer_Tick( object sender, EventArgs e )
{
_Timer.Tick -= Timer_Tick;
_CanExecute = true;
CommandManager.InvalidateRequerySuggested();
}

public ICommand Open
{
get { return new RelayCommand( ExecuteOpen, () => _CanExecute ); }
}

关于c# - 确定的日期时间还剩 4 分钟时启用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34393804/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com