【问题标题】:Prism: have to call RaiseCanExecuteChanged() explicitly棱镜:必须显式调用 RaiseCanExecuteChanged()
【发布时间】:2016-08-15 15:47:16
【问题描述】:

下面是一个非常简单的Prism.Wpf 示例,其中DelegateCommand 同时具有Execute 和CanExecute 代表。

假设CanExecute 依赖于某个属性。似乎 Prism 的 DelegateCommand 不会在此属性更改时自动重新评估 CanExecute 条件,就像 RelayCommand 在其他 MVVM 框架中所做的那样。相反,您必须在属性设置器中显式调用 RaiseCanExecuteChanged()。这会导致在任何重要的视图模型中出现大量重复代码。

有没有更好的办法?

视图模型:

using System;
using Prism.Commands;
using Prism.Mvvm;

namespace PrismCanExecute.ViewModels
{
public class MainWindowViewModel : BindableBase
{
    private string _title = "Prism Unity Application";
    public string Title
    {
        get { return _title; }
        set { SetProperty(ref _title, value); }
    }
    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            SetProperty(ref _name, value);

            // Prism doesn't track CanExecute condition changes?
            // Have to call it explicitly to re-evaluate CanSubmit()
            // Is there a better way?
            SubmitCommand.RaiseCanExecuteChanged();
        }
    }
    public MainWindowViewModel()
    {
        SubmitCommand = new DelegateCommand(Submit, CanSubmit);
    }

    public DelegateCommand SubmitCommand { get; private set; }
    private bool CanSubmit()
    {
        return (!String.IsNullOrEmpty(Name));
    }
    private void Submit()
    {
        System.Windows.MessageBox.Show(Name);
    }

}
}

查看:

<Window x:Class="PrismCanExecute.Views.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:prism="http://prismlibrary.com/"
    Title="{Binding Title}"
    Width="525"
    Height="350"
    prism:ViewModelLocator.AutoWireViewModel="True">
<Grid>
    <!--<ContentControl prism:RegionManager.RegionName="ContentRegion" />-->
    <StackPanel>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Name: " />
            <TextBox Width="150"
                     Margin="5"
                     Text="{Binding Name,  UpdateSourceTrigger=PropertyChanged}"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
            <Button Width="50"
                    Command="{Binding SubmitCommand}"
                    Content="Submit" Margin="10"/>
            <!--<Button Width="50"
                    Content="Cancel"
                    IsCancel="True" Margin="10"/>-->
        </StackPanel>
    </StackPanel>
</Grid>
</Window>

【问题讨论】:

    标签: wpf mvvm prism delegatecommand


    【解决方案1】:

    正如@l33t 解释的那样,这是设计的。如果您希望 DelegateCommand 自动监控 VM 属性的更改,只需使用 delegateCommand 的 ObservesProperty 方法:

    var command = new DelegateCommand(Execute).ObservesProperty(()=> Name);
    

    【讨论】:

      【解决方案2】:

      这是设计使然。它与性能有关。

      不过,您可以将 Prism DelegateCommand 替换为执行您想要的自定义命令。例如。 this 实现似乎可以解决问题。但是,我不建议使用它。如果你有很多命令,你很可能会遇到性能问题。

      另外,请参阅answer。

      【讨论】:

      猜你喜欢
      • 2019-04-03
      • 2013-04-15
      • 2018-05-12
      • 2019-01-25
      • 1970-01-01
      • 2012-03-13
      • 2021-12-03
      • 2019-09-22
      • 1970-01-01
      相关资源
      最近更新 更多