我刚刚开始使用MVVM基金会。我正进入(状态

alt text

我的代码下面:

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;
}

StartView

<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>

代码怎么了?当我尝试单击“增量”按钮时,出现错误

有帮助吗?

解决方案

将基础更改为Raisepropertychanged Line。

基类没有称为计数器的属性

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

MVVM基金会中观察者的评论提到它正在验证公共财产

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top