我试图在列表中的两个项目之间插入一个新行(不同的单元格)。问题是插入新项目时,它不会插入所需的单元格。
例如,我想在第1项和第2项之间插入item5(ItemCell2):
ITEM1(ItemCell1)
ITEM2(ItemCell1)
项目3(ItemCell1)
ITEM4(ItemCell1)
当我插入item5时,单元格采用ItemCell1而不是ItemCell2的形式。
另外,我注意到DemoTemplate中的OnSelectTemplate
只被调用一次。因此,如果列表的第一项是Visible = true
,它会将ItemCell2应用于所有行,即使列表中包含Visible = false
using System;
using System.Collections.ObjectModel;
using MvvmHelpers;
using Xamarin.Forms;
namespace Demo.ViewModels
{
public class ItemViewModel : BaseViewModel
{
public ItemViewModel()
{
var item1 = new Item { ItemName = "Name 1", Visible = false };
var item2 = new Item { ItemName = "Name 2", Visible = true };
var item3 = new Item { ItemName = "Name 3", Visible = false };
var item4 = new Item { ItemName = "Name 4", Visible = false };
var item5 = new Item { ItemName = "Name 5", Visible = false };
List = new ObservableCollection<Item>();
List.Add(item1);
List.Add(item2);
List.Add(item3);
List.Add(item4);
List.Add(item5);
MessagingCenter.Subscribe<Item, Item>(this, "msg", (sender, arg) =>
{
DoSomething(arg);
});
void DoSomething(Item item)
{
var index = List.IndexOf(item);
List.Insert(index, new Item { ItemName = "Name 6", Visible = true} );
}
}
ObservableCollection<Item> list;
public ObservableCollection<Item> List
{
get { return list; }
set { SetProperty(ref list, value); }
}
}
}
档案:
using System;
using System.Windows.Input;
using MvvmHelpers;
using Xamarin.Forms;
namespace Demo.Models
{
public class Item : ObservableObject
{
string itemName;
public string ItemName
{
get { return itemName; }
set { SetProperty(ref itemName, value); }
}
bool visible;
public bool Visible
{
get { return visible; }
set { SetProperty(ref visible, value); }
}
ICommand testCommand;
public ICommand TestCommand => testCommand ??
(
testCommand = new Command(DoSomething)
);
void DoSomething()
{
MessagingCenter.Send(this, "msg", this);
System.Diagnostics.Debug.WriteLine("test");
}
}
}
数据模板:
using System;
using Xamarin.Forms;
namespace Demo.Templates
{
public class DemoTemplate : DataTemplateSelector
{
readonly DataTemplate dataTemplate1;
readonly DataTemplate dataTemplate2;
public DemoTemplate()
{
dataTemplate1 = new DataTemplate(typeof(ItemCell1));
dataTemplate2 = new DataTemplate(typeof(ItemCell2));
}
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
var dataTemplate = dataTemplate1;
var myItem = item as Item;
if(myItem.Visible)
{
dataTemplate = dataTemplate1;
}
else
{
dataTemplate = dataTemplate2;
}
return dataTemplate;
}
}
}
XAML:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Demo.Pages.HomePage">
<ContentPage.Resources>
<ResourceDictionary>
<template:DmoTemplate
x:Key="dataTemplate">
</template:DemoTemplate>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<ListView
CachingStrategy="RecycleElementAndDataTemplate"
ItemTemplate="{StaticResource dataTemplate}"
ItemsSource="{Binding List}"
HasUnevenRows="true"
SeparatorVisibility="None">
</ListView>
</ContentPage.Content>
</ContentPage>
答案 0 :(得分:0)
将CachingStrategy更改为“ CachingStrategy =“ RecycleElement”'解决了该问题。