【问题标题】:Xamarin forms xaml Listview not updating as per collection changesXamarin 形成 xaml Listview 未根据集合更改进行更新
【发布时间】:2015-05-20 21:38:10
【问题描述】:

您好,我正在尝试通过添加和删除项目来更新listView。在listview 中添加项目在 iOS 和 android 中运行良好。但是从中删除任何项目在android上都会出错,它在iOS上完美运行。 在 Android 中:每当我从列表视图中删除一个项目时,它会从集合中删除,但在 UI 上,它会在删除后删除 UI 上存在的最后一个项目,当我再次添加时,它将以前删除的项目添加到列表中,而不是新的新项目。请检查我使用的代码

<ListView  RowHeight="100" HorizontalOptions="End" ItemsSource="{Binding GuidePrices, Mode=OneWay}" >
  <ListView.ItemTemplate>
    <DataTemplate>
      <ViewCell>
        <ViewCell.View>
          <StackLayout  Orientation="Horizontal">
            <controls:BindablePicker ItemsSource="{Binding Prices}" WidthRequest="150" SelectedItem="{Binding SelectedProduct, Mode=TwoWay}" />
            <Button Text="Add" IsVisible="{Binding Action}">
              <b:Interaction.Behaviors>
                <b:BehaviorCollection>
                  <b:EventToCommand  CommandNameContext="{b:RelativeContext CampsiteViewPage}"
                        EventName="Clicked" 
                        CommandName="AddPriceListCommand"
                        CommandParameter="{Binding}" /> 
                </b:BehaviorCollection>
              </b:Interaction.Behaviors>
            </Button>
             <Button Text="Delete" IsVisible="{Binding Action, Converter={StaticResource cnvInvert}}">
              <b:Interaction.Behaviors>
                <b:BehaviorCollection>
                  <b:EventToCommand  CommandNameContext="{b:RelativeContext CampsiteViewPage}"
                        EventName="Clicked" 
                        CommandName="DeletePriceListCommand"
                        CommandParameter="{Binding}" /> 
                </b:BehaviorCollection>
              </b:Interaction.Behaviors>
            </Button>
          </StackLayout>
        </ViewCell.View>
      </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

--添加删除命令的ViewModel代码

this.AddPriceListCommand = new Command((s) =>
        {
            if (_guidePrices != null)
                {
                _guidePrices.Add(new PricesListModel() { ListId = Guid.NewGuid(), Action = false, Prices = _campsite.Guides[0].Prices });
                }
        });

        this.DeletePriceListCommand = new Command((sender) => {

        var  _priseList = (from priceList in _guidePrices
                           where priceList == (PricesListModel)sender
                   select priceList).ToList();
        _guidePrices.RemoveAt(_guidePrices.IndexOf((PricesListModel)sender));

            });

我无法找到原因,为什么这在 android 上会像预期的那样在 ios 上运行。

【问题讨论】:

  • 您使用的是什么 Xamarin.Forms 版本?
  • 这里是我的 xamarin VS 配置 Xamarin 3.9.519.0 (5667ef4) Xamarin.Android 4.20.0.37 Xamarin.iOS 8.8.1.0 Xamarin.iOS Unified Migration 1.0

标签: xaml listview xamarin xamarin.android xamarin.forms


【解决方案1】:

_guidePrices 的类型是什么?从 List&lt;T&gt; 中删除项目不会更新 UI。您需要使用ObservableCollction&lt;T&gt; 或引发 PropertyChanged 事件通知 UI 进行更新

【讨论】:

  • 是的,我正在使用 'ObservableColction ',我的列表视图也会更新,但是每当我删除列表中的中间项目时,它会从 UI 中删除最后一个项目。此外,当我添加新项目而不是添加新项目时,它会在最后添加已删除的项目。在可观察的集合中删除确定。它按预期在 iOS 中运行。不明白为什么使用 android 平板电脑。
  • Prashant 对 android 中的这种行为的任何猜测,以便我可以解决。
  • 我不知道,我得在我的机器上试试
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-02-25
  • 2014-01-17
  • 1970-01-01
  • 2016-10-03
  • 2019-06-24
  • 1970-01-01
相关资源
最近更新 更多