事件要命令Issua Xamarin.Forms

问题描述:

HEJ,事件要命令Issua Xamarin.Forms

我已绑定到的ObservableCollection一个ListView,并使用事件即时消息,而不是命令ItemTapped的。我注意到一个非常奇怪的行为,如果我添加一个项目到我的集合我的应用程序崩溃与下面的异常异常已被调用的目标抛出。 堆栈跟踪:http://pastebin.com/Qj77Q5j6

现在,如果我集合更改为正常的列表在App犯规崩溃了,但心不是列表一个选项我,因为我需要加入项目时得到更新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> 

如果删除该事件的命令行为清单可以作为excpected,但我尝试不会打破MVVM模式。

事件到的命令行为:https://blog.xamarin.com/turn-events-into-commands-with-behaviors/

+0

哪里是你的代码'EventToCommandBehavior'? – valdetero

这听起来像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> 
+0

感谢您的回答,这真的很奇怪,因为当我使用aList它的工作原理没有更新....我可以使用这个像MVVM模式的ListView? –

+0

是的,使用我已经使用的示例检查更新后的答案。 – MohamedHamza

+0

感谢您的时间,但我又有同样的问题。 –