C# 如何使用Delegatecommand.ObservesProperty观察多个属性

C# 如何使用Delegatecommand.ObservesProperty观察多个属性,c#,wpf,mvvm,prism,C#,Wpf,Mvvm,Prism,我希望观察viewmodel中的多个属性以检查CanExecute方法 问题: class MyViewModel { public int myproperty1 { get; set; } public int myproperty2 { get; set; } public DelegateCommand MyCommand { get; set; } public MyVi

我希望观察viewmodel中的多个属性以检查CanExecute方法

问题:

class MyViewModel
        {
            public int myproperty1 { get; set; }
            public int myproperty2 { get; set; }

            public DelegateCommand MyCommand { get; set; }

            public MyViewModel()
            {
                MyCommand = new DelegateCommand(MyCommandMethod,CanExecuteMyCommandMethod);
                MyCommand.ObservesProperty((() => myproperty1));
                // line below doesnt work Exeception "Value is already observed". How to register more properties to observe?
                MyCommand.ObservesProperty((() => myproperty2));

            }

            private bool CanExecuteMyCommandMethod()
            {
                throw new NotImplementedException();
            }

            private void MyCommandMethod()
            {
                throw new NotImplementedException();
            }
        }
如何注册更多属性

示例:

class MyViewModel
        {
            public int myproperty1 { get; set; }
            public int myproperty2 { get; set; }

            public DelegateCommand MyCommand { get; set; }

            public MyViewModel()
            {
                MyCommand = new DelegateCommand(MyCommandMethod,CanExecuteMyCommandMethod);
                MyCommand.ObservesProperty((() => myproperty1));
                // line below doesnt work Exeception "Value is already observed". How to register more properties to observe?
                MyCommand.ObservesProperty((() => myproperty2));

            }

            private bool CanExecuteMyCommandMethod()
            {
                throw new NotImplementedException();
            }

            private void MyCommandMethod()
            {
                throw new NotImplementedException();
            }
        }
更新:

class MyViewModel
        {
            public int myproperty1 { get; set; }
            public int myproperty2 { get; set; }

            public DelegateCommand MyCommand { get; set; }

            public MyViewModel()
            {
                MyCommand = new DelegateCommand(MyCommandMethod,CanExecuteMyCommandMethod);
                MyCommand.ObservesProperty((() => myproperty1));
                // line below doesnt work Exeception "Value is already observed". How to register more properties to observe?
                MyCommand.ObservesProperty((() => myproperty2));

            }

            private bool CanExecuteMyCommandMethod()
            {
                throw new NotImplementedException();
            }

            private void MyCommandMethod()
            {
                throw new NotImplementedException();
            }
        }
PropertChanged事件由PropertChanged.Fody INPC完成


ObservesProperty将提高下一个ecuteChanged。 第二个属性(已观察到“引发异常”值)

附加问题: 观察属性((()=>myproperty)

允许多个或单个属性?
如果其他人确认可以进行多次注册?

像您这样为另一个属性调用
observeProperty
方法应该可以。但是您需要在源属性的设置器中为要引发的
CanExecuteChanged
事件引发
属性更改事件

请参考以下示例代码

public class MyViewModel : BindableBase
{
    private int _myproperty1;
    public int myproperty1
    {
        get { return _myproperty1; }
        set { _myproperty1 = value; RaisePropertyChanged(); }
    }

    private int _myproperty2;
    public int myproperty2
    {
        get { return _myproperty2; }
        set { _myproperty2 = value; RaisePropertyChanged(); }
    }

    public DelegateCommand MyCommand { get; set; }

    public MyViewModel()
    {
        MyCommand = new DelegateCommand(MyCommandMethod, CanExecuteMyCommandMethod);
        MyCommand.ObservesProperty((() => myproperty1));
        MyCommand.ObservesProperty((() => myproperty2));
    }

    private bool CanExecuteMyCommandMethod()
    {
        return true;
    }

    private void MyCommandMethod()
    {

    }
}

像您正在做的那样,为另一个属性调用
observeProperty
方法应该可以。但是您需要在要引发的
CanExecuteChanged
事件的源属性的setters中引发
PropertyChanged
事件

请参考以下示例代码

public class MyViewModel : BindableBase
{
    private int _myproperty1;
    public int myproperty1
    {
        get { return _myproperty1; }
        set { _myproperty1 = value; RaisePropertyChanged(); }
    }

    private int _myproperty2;
    public int myproperty2
    {
        get { return _myproperty2; }
        set { _myproperty2 = value; RaisePropertyChanged(); }
    }

    public DelegateCommand MyCommand { get; set; }

    public MyViewModel()
    {
        MyCommand = new DelegateCommand(MyCommandMethod, CanExecuteMyCommandMethod);
        MyCommand.ObservesProperty((() => myproperty1));
        MyCommand.ObservesProperty((() => myproperty2));
    }

    private bool CanExecuteMyCommandMethod()
    {
        return true;
    }

    private void MyCommandMethod()
    {

    }
}

ObservesProperty将引发Nexecutechanged。InotifyPropertyChanged由Propertychanged.Fody INPC完成。第二个属性observe registration引发异常”值已被观察到)。好的,那么我必须找出是谁进行了注册,可能是一个智能的第三方框架控制元素。标记为answeredInside
public MyViewModel()
它应该是
MyCommand=new DelegateCommand(MyCommand方法,CanExecuteMyCommand方法)。observeProperty((()=>myproperty1))。observeProperty(((()=>myproperty2))
ObservesProperty将引发Nexecutechanged。InotifyPropertyChanged由Propertychanged.Fody INPC完成。已观察到第二个属性observe注册引发异常”值)。好的,那么我必须找出是谁进行了注册,可能是一个智能的第三方框架控制元素。标记为answeredInside
public MyViewModel()
它应该是
MyCommand=new DelegateCommand(MyCommand方法,CanExecuteMyCommand方法)。observeProperty((()=>myproperty1))。observeProperty(((()=>myproperty2));