无法将lambda表达式转换为类型'string',因为它不是具有NotifyOfPropertyChange的委托类型

时间:2014-04-04 02:15:52

标签: c# lambda caliburn.micro

当我尝试使用NotifyOfPropertyChange()来使用以下代码更新UI时:

public string Username    
{         
    get { return _Username;}  

    set {
        _Username = value;

        NotifyOfPropertyChange(() => Username);
        NotifyOfPropertyChange((x) => CanLogOn(x));
    }
}

public bool CanLogOn(object parameter)
{
    return !string.IsNullOrEmpty(Username); 
}

Intellisence显示错误:(BTW,已引用System.Linq)

Cannot convert lambda expression to type 'string' because it is not a delegate type

我是C#和CM的新手,请帮帮我。

2 个答案:

答案 0 :(得分:2)

查看NotifyOfPropertyChange的文档,您只需要返回一个属性。框架使用反射来将实际字符串传递给" PropertyChanged"

通过返回一个字符串,你破坏了这个系统(这对普通的INoifyPropertyChanged也不行。)

您需要通知一个返回" CanLogOn"的结果的实际属性,而不是尝试通知不存在的属性。

答案 1 :(得分:1)

如果没有看到它,我猜你的NotifyOfPropertyChange就像Microsoft.Prism.ViewModel.NotificationObject中的RaisePropertyChanged一样。

您可以做的是添加一个bool属性,该属性返回CanLogOn(UserName)的结果并通知它。

public string Username    
{         
    get { return _Username;}  

    set {
        _Username = value;

        NotifyOfPropertyChange(() => Username);
        NotifyOfPropertyChange(() => Can());
    }
}

public bool Can
{
    get { return CanLogOn(Username); }
}

public bool CanLogOn(object parameter)
{
    return !string.IsNullOrEmpty(Username); 
}