【发布时间】:2026-01-08 16:35:02
【问题描述】:
快速说明,以免浪费任何人的时间。从 nuget 安装 MVVMLight 时,我最终收到错误 null : The term 'null' is not recognized as the name of a cmdlet, function, script file, or operable program。 MVVMLight 似乎工作正常,除了下面将要描述的问题,但我想提一下以防万一。
问题
我遇到了命令执行完成后按钮未重新启用的问题。如果执行速度非常快,它们似乎有时会起作用,但任何需要一段时间的操作似乎都不会起作用。比赛条件的尖叫声。
我正在使用 Task 执行冗长的操作,以便 UI 可以更新。 Task的第一步也是最后一步是适当地翻转IsBusy(其中每个CanExecute方法returns !IsBusy;)
我已经构建了一个简单的示例,它将使用 Thread.Sleep 来模拟慢速操作,它很好地展示了这个问题。 WaitOneSecondCommand 似乎间歇性地工作。 WaitTenSecondsCommand 和 WaitThirtySecondsCommand 永远不会工作。
不起作用我的意思是按钮保持禁用状态,直到我单击表单上的某个位置。
我尝试过的事情
我已经进行了大量研究,到目前为止我尝试过的解决方案并没有改变这种行为。我最终导致暴力破解所有不同的“修复”,以防我误解。
我尝试过的一件事是扩展 RelayCommands 以提高属性更改。举个例子:
发件人:
public RelayCommand WaitOneSecondCommand {
get; set;
}
收件人:
public RelayCommand WaitTenSecondsCommand {
get {
return _waitTenSecondsCommand;
}
set {
_waitTenSecondsCommand = value;
RaisePropertyChanged();
}
}
我没想到它会起作用,但我想尝试一下。我也尝试添加WaitTenSecondsCommand.RaiseCanExecuteChanged();
我也尝试将 WaitTenSecondsCommand.RaiseCanExecuteChanged() 添加到 CommandExecute 方法中,但这也没有改变任何东西。
private void WaitTenSecondsCommandExecute() {
Task.Run(() => {
IsBusy = true;
Thread.Sleep(10000);
IsBusy = false;
WaitTenSecondsCommand.RaiseCanExecuteChanged();
});
}
我还阅读了有关 CommandManager 的信息,因此我也添加了 CommandManager.InvalidateRequerySuggested()。我将它添加到 IsBusy 认为这会非常垃圾,但我认为它会消除任何疑问
这又是竞争条件的味道,我在这里使用 Task,但是这些任务不能同时运行,并且由于使用了 IsBusy 标志而不能相互冲突。
完整代码
这是一个基本的 WPF 应用程序,使用 .Net 4.6.1 和从 Nuget 安装的 MVVMLight 5.2.0。
MainWindow.xaml
<Window x:Class="*Example.View.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" MinHeight="100" Width="150" ResizeMode="NoResize" SizeToContent="Height"
DataContext="{Binding Source={StaticResource Locator}, Path=Main}">
<StackPanel>
<Button Height="70" Margin="5" Command="{Binding WaitOneSecondCommand}">Wait 1 Second</Button>
<Button Height="70" Margin="5" Command="{Binding WaitTenSecondsCommand}">Wait 10 Seconds</Button>
<Button Height="70" Margin="5" Command="{Binding WaitThirtySecondsCommand}">Wait 30 Seconds</Button>
</StackPanel>
<Grid>
<Label HorizontalAlignment="Left" Width="84">IsBusy:</Label>
<TextBox IsReadOnly="True" HorizontalAlignment="Right" Width="45" Text="{Binding IsBusy}" Margin="4,5,5,5" />
</Grid>
</Window>
我为 IsBusy 添加了绑定,以清楚地表明它正在正确更新。这也是了解 10 秒和 30 秒命令何时完成的唯一可靠方法。我不希望有任何 MessageBox 迫使您单击 OK 导致 CanExecute 更新。这是一个创可贴修复。
MainViewModel.cs
public class MainViewModel : ViewModelBase {
private bool _isBusy;
private RelayCommand _waitTenSecondsCommand;
public MainViewModel() {
WaitOneSecondCommand = new RelayCommand(WaitOneSecondCommandExecute, WaitOneSecondCommandCanExecute);
WaitTenSecondsCommand = new RelayCommand(WaitTenSecondsCommandExecute, WaitTenSecondsCommandCanExecute);
WaitThirtySecondsCommand = new RelayCommand(WaitThirtySecondsCommandExecute, WaitThirtySecondsCommandCanExecute);
}
public RelayCommand WaitOneSecondCommand {
get; set;
}
public RelayCommand WaitTenSecondsCommand {
get {
return _waitTenSecondsCommand;
}
set {
_waitTenSecondsCommand = value;
RaisePropertyChanged();
WaitTenSecondsCommand.RaiseCanExecuteChanged();
}
}
public RelayCommand WaitThirtySecondsCommand {
get; set;
}
public bool IsBusy {
get {
return _isBusy;
}
set {
_isBusy = value;
RaisePropertyChanged();
CommandManager.InvalidateRequerySuggested();
}
}
private void WaitOneSecondCommandExecute() {
Task.Run(() => {
IsBusy = true;
Thread.Sleep(1000);
IsBusy = false;
});
}
private void WaitTenSecondsCommandExecute() {
Task.Run(() => {
IsBusy = true;
Thread.Sleep(10000);
IsBusy = false;
WaitTenSecondsCommand.RaiseCanExecuteChanged();
});
}
private void WaitThirtySecondsCommandExecute() {
Task.Run(() => {
IsBusy = true;
Thread.Sleep(30000);
IsBusy = false;
});
}
private bool WaitOneSecondCommandCanExecute() {
return !IsBusy;
}
private bool WaitTenSecondsCommandCanExecute() {
return !IsBusy;
}
private bool WaitThirtySecondsCommandCanExecute() {
return !IsBusy;
}
}
请注意,在视图模型中,我只在 WaitTenSeconds 上放置了一个支持字段,以展示它不会改变行为。
【问题讨论】:
标签: c# wpf task mvvm-light relaycommand