事件到命令发布Xamarin.Forms

本文关键字:Xamarin Forms 命令 事件 | 更新日期: 2023-09-27 18:04:32

Hej,

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

现在,如果我将集合更改为正常列表应用程序不再崩溃,但列表不是我的一个选项,因为我需要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>

如果我删除事件到命令的行为,列表按预期工作,但我试图不打破MVVM模式。

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

事件到命令发布Xamarin.Forms

听起来像是Xamarin的一个bug。表格,我发现它已经在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>