Hej,
我有一个绑定到ObservableCollection的ListView,我使用Event To Command而不是ItemTapped。我注意到一个非常奇怪的行为,如果我将一个Item添加到我的Collection我的应用程序崩溃以下异常调用的目标抛出异常。 StackTrace:http://pastebin.com/Qj77Q5j6
现在如果我将集合更改为正常列表,应用程序不再崩溃,但列表不是我的选项,因为我需要ListView在添加项目时获得更新。
的ListView:
<ListView x:Name="ListViewPerson"
ItemsSource="{Binding PersonCollection, Mode=TwoWay}"
Grid.Column="0"
SeparatorColor="Silver"
ItemTemplate="{StaticResource TemplateSelector}">
<ListView.Behaviors>
<commands:EventToCommandBehavior EventName="ItemTapped" Command="{Binding ListViewAngebotItemTappedCommand}" EventArgsConverter="{StaticResource ItemTappedConverter}" />
</ListView.Behaviors>
</ListView>
如果我删除了Event to Command行为,List就像被驱逐的那样工作,但我试图不破坏MVVM模式。
命令行为事件:https://blog.xamarin.com/turn-events-into-commands-with-behaviors/
答案 0 :(得分:0)
这听起来像是Xamarin.Forms中的一个错误,我发现它已经在Bugzilla上提交了它:https://bugzilla.xamarin.com/show_bug.cgi?id=26418。
我对Xamarin列表视图的体验非常糟糕,而不是我使用自定义中继器
public class CustomRepeater : StackLayout
{
/// <summary>
/// The Item template property
/// </summary>
public static readonly BindableProperty ItemTemplateProperty = BindableProperty.Create("ItemTemplate", typeof(DataTemplate), typeof(CustomRepeater), null, propertyChanged: (bindable, oldvalue, newvalue) => ((CustomRepeater)bindable).OnSizeChanged());
/// <summary>
/// Gets or sets the item template
/// </summary>
public DataTemplate ItemTemplate
{
get { return (DataTemplate)GetValue(ItemTemplateProperty); }
set { this.SetValue(ItemTemplateProperty, value); }
}
public void OnSizeChanged()
{
this.ForceLayout();
}
public ScrollView RootScrollView { private set; get; }
public StackLayout MainStackLayout { private set; get; }
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
Children.Clear();
RootScrollView = new ScrollView();
MainStackLayout = new StackLayout();
MainStackLayout.Children.Clear();
IList list =BindingContext as IList;
if (list != null)
{
foreach (var i in list)
{
var child = this.ItemTemplate.CreateContent() as View;
if (child == null)
{
return;
}
child.BindingContext = i;
MainStackLayout.Children.Add(child);
}
Children.Add(MainStackLayout);
}
}
在Xaml中:
<UserControls:CustomRepeater x:Name="repeaterUC" Grid.Row="1" BindingContext="{Binding CurrentChallenge.Over12FormQuestionsCollection}" >
<UserControls:CustomRepeater.ItemTemplate>
<DataTemplate>
<StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Label Text="{Binding Name}" TextColor="#223568" />
<Label Text="{Binding SelectedAnswersText}" FontAttributes="Italic" TextColor="#223568" LineBreakMode="WordWrap"/>
</StackLayout>
</DataTemplate>
</UserControls:CustomRepeater.ItemTemplate>
</UserControls:CustomRepeater>