将SelectedItem设置为null不会重置所选的List项目

时间:2019-02-06 12:11:11

标签: xamarin mvvm xamarin.forms

在我的视图模型中设置{ "name": "app2", "version": "0.0.0", "scripts": { "ng": "ng", "start": "ng serve", "build": "ng build", "test": "ng test", "lint": "ng lint", "e2e": "ng e2e" }, "private": true, "dependencies": { "@angular/animations": "~7.1.0", "@angular/common": "~7.1.0", "@angular/compiler": "~7.1.0", "@angular/core": "~7.1.0", "@angular/forms": "~7.1.0", "@angular/platform-browser": "~7.1.0", "@angular/platform-browser-dynamic": "~7.1.0", "@angular/router": "~7.1.0", "@ng-bootstrap/ng-bootstrap": "^4.0.2", "bootstrap": "^4.2.1", "core-js": "^2.5.4", "jquery": "^3.3.1", "ngx-swiper-wrapper": "^7.2.1", "popper": "^1.0.1", "rxjs": "~6.3.3", "tslib": "^1.9.0", "zone.js": "~0.8.26" }, "devDependencies": { "@angular-devkit/build-angular": "~0.11.0", "@angular/cli": "~7.1.2", "@angular/compiler-cli": "~7.1.0", "@angular/language-service": "~7.1.0", "@types/node": "~8.9.4", "@types/jasmine": "~2.8.8", "@types/jasminewd2": "~2.0.3", "codelyzer": "~4.5.0", "jasmine-core": "~2.99.1", "jasmine-spec-reporter": "~4.2.1", "karma": "~3.1.1", "karma-chrome-launcher": "~2.2.0", "karma-coverage-istanbul-reporter": "~2.0.1", "karma-jasmine": "~1.1.2", "karma-jasmine-html-reporter": "^0.2.2", "protractor": "~5.4.0", "ts-node": "~7.0.0", "tslint": "~5.11.0", "typescript": "~3.1.6" } } 后,不会重置ListView的所选项目。

在我的XAML中,SelectedItem=null绑定到我的视图模型的ListView.SelectedItem

SelectedItem

SelectedItem="{Binding SelectedItem}" 的定义如下

SelectedItem

设置后

LocalProductSummary _selectedItem;

public LocalProductSummary SelectedItem
{
    get { return _selectedItem; }
    set
    {
        if (value != null)
        {
            _selectedItem = value;
            OnPropertyChanged();
        }
    }
} 

列表项未重置。

如何在MVVM中解决此问题?

1 个答案:

答案 0 :(得分:3)

SelectedItem未设置为空,因为您在setter方法中具有空检查条件。

问题在这里

if (value != null)
{
    SelectedItem = value;
    OnPropertyChanged();
}

您正在检查值是否不为null,然后设置该值。只需删除条件并检查。

解决方案:

LocalProductSummary _selectedItem;
public LocalProductSummary SelectedItem
{
    get { return _selectedItem; }
    set
    {
        _selectedItem = value;
        OnPropertyChanged();
    }
}