C# 当使用CommandManager.InvalidateRequestSuggested()时?
有时UI未更新,因此需要使用命令刷新命令。(RelayCommand)。例如,启用或禁用按钮。 我在那里寻找如何解决这个问题,我找到了这个解决方案CommandManager.invalidateRequestSuggested()。 这是可行的,但我发现,“CommandManager.invalidateRequestSuggested()尝试验证所有命令,这是完全无效的(在您的情况下是缓慢的)-在每次更改中,您都要求每个命令重新检查其CanExecute()” 我做过一些应用程序(代码隐藏与我遇到的问题非常相似),我遇到了这个问题。 当方法执行结束时,按钮没有更新,我需要在UI上单击一次C# 当使用CommandManager.InvalidateRequestSuggested()时?,c#,.net,wpf,mvvm-light,C#,.net,Wpf,Mvvm Light,有时UI未更新,因此需要使用命令刷新命令。(RelayCommand)。例如,启用或禁用按钮。 我在那里寻找如何解决这个问题,我找到了这个解决方案CommandManager.invalidateRequestSuggested()。 这是可行的,但我发现,“CommandManager.invalidateRequestSuggested()尝试验证所有命令,这是完全无效的(在您的情况下是缓慢的)-在每次更改中,您都要求每个命令重新检查其CanExecute()” 我做过一些应用程序(代码隐藏
...
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
...
...
private ICommand _downloadCommand;
public ICommand DownloadCommand
{
get
{
return _downloadCommand ?? (_downloadCommand = new RelayCommand(Download, () => IsEnabled));
}
}
private ICommand _cancelDownloadCommand;
public ICommand CancelDownloadCommand
{
get
{
return _cancelDownloadCommand ?? (_cancelDownloadCommand = new RelayCommand(CancelDownload, () => IsEnabledCancel));
}
}
private bool _isEnabled = true;
private bool IsEnabled
{
get { return _isEnabled; }
set
{
_isEnabled = value;
RaisePropertyChanged();
}
}
private bool _isEnabledCancel;
private bool IsEnabledCancel
{
get { return _isEnabledCancel; }
set
{
_isEnabledCancel = value;
RaisePropertyChanged();
}
}
private async void Download()
{
IsEnabled = false;
IsEnabledCancel = true;
await Task.Run(() =>
{
...
...
...
...
...
});
IsEnabled = true;
IsEnabledCancel = false;
}
.XAML
<DockPanel Dock="Top">
<Button Margin="5" VerticalAlignment="Top" HorizontalAlignment="Center" Command="{Binding CancelDownloadCommand}" FontSize="20"
Background="Transparent" BorderThickness="2" BorderBrush="{StaticResource AccentColorBrush4}" ToolTip="Cancel"
DockPanel.Dock="Right">
<StackPanel Orientation="Horizontal">
<Image Source="Images/48x48/Error.png" Height="48" Width="48"/>
<Label Content="{Binding ToolTip, RelativeSource={RelativeSource AncestorType={x:Type Button}}}" FontFamily="Segoe UI Light"/>
</StackPanel>
</Button>
<Button Margin="5" VerticalAlignment="Top" HorizontalAlignment="Center" Command="{Binding DownloadCommand}" FontSize="20"
Background="Transparent" BorderThickness="2" BorderBrush="{StaticResource AccentColorBrush4}" ToolTip="Download"
DockPanel.Dock="Right">
<StackPanel Orientation="Horizontal">
<Image Source="Images/48x48/Download.png" Height="48" Width="48"/>
<Label Content="{Binding ToolTip, RelativeSource={RelativeSource AncestorType={x:Type Button}}}" FontFamily="Segoe UI Light"/>
</StackPanel>
</Button>
</DockPanel>