首页 > 解决方案 > Xamarin 表单 - 无法更新选取器

问题描述

以下 Xamarin Forms 应用程序包含一个Picker和一个CollectionView绑定到相同List动物 (ChickenCow) 的控件。当我按下升级按钮时,动物应该升级到Eagleand Elephant。动物已升级,但 Picker 并未反映更改。CollectionView作品很好。

我在这里做错了什么?如果需要,可以在 Github上找到该项目的源代码。

点击查看演示

伊姆古尔

主页.xaml

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:d="http://xamarin.com/schemas/2014/forms/design"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 mc:Ignorable="d"
                 x:Class="SampleApp.MainPage">

        <StackLayout Margin="0,100,0,0">
            <Button Text="Upgrade Animals" 
                    Margin="10"
                    HorizontalOptions="Center"
                    VerticalOptions="Start"
                    Command="{Binding Upgrade}" />
            <Picker ItemsSource="{Binding Animals}" />
            <CollectionView ItemsSource="{Binding Animals}" />
        </StackLayout>

    </ContentPage>

MainPageModel.cs

    public class MainPageModel : INotifyPropertyChanged
    {
        public MainPageModel()
        {
            _Animals = new List<string>() { "Chicken", "Cow" };
        }

        private List<string> _Animals;
        public List<string> Animals
        {
            get { return _Animals; }
            set
            {
                _Animals = value;

                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Animals"));
            }
        }

        public Command Upgrade
        {
            get
            {
                return new Command(_ =>
                {
                    Animals = new List<string>() { "Eagle", "Elephant" };
                });
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

更新

上面的代码正在运行 XF 4.3.0.908675。我降级到 4.2.0.778463,问题就消失了。这可能是最新 XF 版本中引入的一个新错误。

标签: xamarinxamarin.forms

解决方案


该问题已在报告的频道上得到确认:Issue 8177,我已经尝试了 Xamarin.Forms(4.4.0.991210-pre2) 的预发布版本,现在绑定在 UWP 上工作正常。


推荐阅读