【问题标题】:Xamarin Forms - can't update PickerXamarin 表单 - 无法更新选取器
【发布时间】:2020-04-22 00:58:05
【问题描述】:

以下 Xamarin Forms 应用包含一个 Picker 和一个 CollectionView 控件绑定到相同的动物 ListChickenCow)。当我按下升级按钮时,动物应该升级到EagleElephant。动物已升级,但 Picker 并未反映更改。 CollectionView 工作正常。

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

点击查看演示

Imgur

MainPage.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 版本中引入的新错误。

【问题讨论】:

  • 诚然是一种解决方法,但您可以手动清除并将新项目重新添加到选择器
  • 谢谢,实际上我已经尝试过了。同样的问题。 CollectionView 更新正常。选择器没有。
  • 你的意思是你从代码中的选择器中删除了项目并且项目仍然存在?
  • Picker 和 CollectionView 都绑定到同一个列表。在上面的示例中,我创建了一个新列表,但我尝试了 Animals.Clear() 然后一一添加。两种方法都有相同的问题。我更新了答案,但这似乎是一个 XF 错误。我降级到 4.2.0.778463,问题就消失了。
  • 出于好奇,您是否启用了热重载?我有最新的 xf,选择器工作正常

标签: xamarin xamarin.forms


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我最近一直在处理一个类似的问题,它似乎是一个 XF 问题。

    我通过不将列表设置为“new List()”而是创建一个单独的列表然后将其附加到绑定列表来解决了我的应用程序中的问题。

    在你的命令中,你应该试试这个: 列表 newList = new List() { "Eagle", "Elephant" }; 动物 = 新列表;

    这可能不适用于您的示例,但这确实适用于我。

    【讨论】:

    • 是的,奇怪的是有效。我只是想看看,但它对我不起作用。
    • 选择器没有更新似乎存在多个问题 - 我使用的 Android 似乎没有完全相同的故障。我的实现涉及使用 .AddRange,然后在某些情况下,我需要使用 .Remove 删除条目。升级 XF 后,这不再更新选择器,但使用我上面提到的实现解决了我的问题。
    猜你喜欢
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多