【问题标题】:MVVM Foundation: Assertion Failed Error: Invalid Property NameMVVM Foundation:断言失败错误:无效的属性名称
【发布时间】:2010-09-25 10:09:11
【问题描述】:

我刚刚开始使用 MVVM Foundation。我得到了

我的代码如下:

StartViewModel

class StartViewModel : ObservableObject
{
    public StartViewModel() {
        _counter = 0;
    }

    public ICommand IncrementCommand
    {
        get { return _incrementCommand ?? (_incrementCommand = new RelayCommand(() => ++Counter)); }
    }

    protected int Counter { 
        get { return _counter; } 
        set {
            _counter = value;
            base.RaisePropertyChanged("Counter");
        }
    }

    protected int _counter;
    protected RelayCommand _incrementCommand;
}

开始视图

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="50*" />
        <RowDefinition Height="250*" />
    </Grid.RowDefinitions>
    <Button Content="Increment" Grid.Row="0" Command="{Binding IncrementCommand}" />
    <TextBlock Padding="5" Text="{Binding Counter}" Grid.Row="1" />
</Grid>

代码有什么问题?当我尝试单击增量按钮时出现错误

【问题讨论】:

    标签: c# mvvm mvvm-foundation


    【解决方案1】:

    在 RaisePropertyChanged 行上将基础更改为此。

    基类没有名为 Counter 的属性

    编辑:也许是因为您的财产受到保护而不是公开

    MVVM Foundation 中 ObservableObject 中的 cmets 提到它正在验证公共财产

    【讨论】:

    • 我仍然遇到同样的错误。但是如果是因为Counter不存在,那么为什么在示例项目中,NumberViewModel可以调用base.RaisePropertyChanged("Value");
    • 谢谢。我的一处房产也有类似的问题。问题是该属性是“受保护”而不是“公共”
    猜你喜欢
    • 1970-01-01
    • 2012-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多