【发布时间】: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