我有一个带有 16 个按钮的 WPF 表单。当我的视图模型初始化时,我需要将所有 16 个设置为 RelayCommand 对象。这就是我的 Initialize() 方法所做的一切,但这会导致代码分析错误 CA1502:AvoidExcessiveComplexity。
这是抑制 CA 警告的好案例,还是有更优雅的方法来设置这些命令而不会导致 CA 违规?
[SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity", Justification = "Simply setting the commands")]
private void Initialize()
{
this.AddApplicationCommand = new RelayCommand(_ => AddApplication());
this.DeleteApplicationCommand = new RelayCommand(_ => DeleteApplication(), _ => ApplicationIsSelected);
this.RefreshApplicationsCommand = new RelayCommand(_ => RefreshApplications());
this.SaveApplicationCommand = new RelayCommand(_ => SaveApplication(), _ => ApplicationIsSelected);
this.ForceInstallationCommand = new RelayCommand(_ => ForceInstallation(), _ => ApplicationIsSelected);
this.DeleteForceInstallationCommand = new RelayCommand(_ => DeleteForceInstallation(), _ => ApplicationIsSelectedAndForceExists());
this.AddTaskCommand = new RelayCommand(_ => AddTask(), _ => ApplicationIsSelected);
this.EditTaskCommand = new RelayCommand(_ => EditTask(), _ => TaskIsSelected());
this.DeleteTaskCommand = new RelayCommand(_ => DeleteTask(), _ => TaskIsSelected());
this.ImportTasksCommand = new RelayCommand(_ => ImportTasks(), _ => ApplicationIsSelected);
this.ExportTasksCommand = new RelayCommand(_ => ExportTasks(), _ => TaskIsSelected());
this.ImportLegacyTasksCommand = new RelayCommand(_ => ImportLegacyTasks(), _ => ApplicationIsSelected);
this.MoveTaskUpCommand = new RelayCommand(_ => MoveRowUp(), _ => TaskIsSelected());
this.MoveTaskDownCommand = new RelayCommand(_ => MoveRowDown(), _ => TaskIsSelected());
this.AddVariableGroupCommand = new RelayCommand(_ => AddVariableGroup());
this.RemoveVariableGroupCommand = new RelayCommand(_ => RemoveVariableGroup(), _ => VariableGroupIsSelected());
}