繁体   English   中英

命令Issua Xamarin.Forms的事件

[英]Event To Command Issua Xamarin.Forms

j

我有一个ListView绑定到一个ObservableCollection,并且我使用Event To Command而不是ItemTapped。 我注意到一个非常奇怪的行为,如果我将一个Item添加到我的Collection中,则我的应用程序崩溃,并出现以下异常:调用的目标抛出了Exception异常。 StackTrace: http//pastebin.com/Qj77Q5j6

现在,如果我将Collection更改为普通列表,则应用程序不再崩溃,但是list对我来说不是一个选项,因为添加项目时我需要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中的错误,我发现它已经在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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM