【问题标题】:Event To Command Issua Xamarin.Forms事件命令 Issua Xamarin.Forms
【发布时间】:2019-04-01 13:52:38
【问题描述】:

嘿嘿,

我有一个绑定到 ObservableCollection 的 ListView,并且我使用 Event To Command 而不是 ItemTapped。我注意到一个非常奇怪的行为,如果我将一个项目添加到我的集合中,我的应用程序崩溃并出现以下异常异常已被调用目标抛出。 堆栈跟踪:http://pastebin.com/Qj77Q5j6

现在,如果我将 Collection 更改为普通列表,应用程序不会再崩溃,但列表对我来说不是一个选项,因为我需要在添加项目时更新 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/

【问题讨论】:

标签: c# xamarin xamarin.forms


【解决方案1】:

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

【讨论】:

  • 感谢您的回答,这真的很奇怪,因为当我使用 aList 时它无需更新即可工作......我可以像使用 MVVM 模式的 ListView 一样使用它吗?
  • 是的,用我已经在使用的样本检查更新的答案。
  • 感谢您的宝贵时间,但我又遇到了同样的问题。
猜你喜欢
  • 2018-11-06
  • 1970-01-01
  • 1970-01-01
  • 2017-02-10
  • 2018-07-26
  • 2015-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多