StackLayout Itemsource throws XamlCTask-error on two labels

fsulserfsulser Member ✭✭
edited February 27 in Xamarin.Forms

Having the following problem. When I put in my xaml:

        <StackLayout BindableLayout.ItemsSource="{Binding Spielplan}">
            <BindableLayout.ItemTemplate>
                <DataTemplate>
                    <Label Text="{Binding Datetime}" HorizontalOptions="FillAndExpand" />
                    <Label Text="{Binding Heim}" HorizontalOptions="FillAndExpand" />
               </DataTemplate>
            </BindableLayout.ItemTemplate>
        </StackLayout>

I get this error during compilation:
Error MSB4018: The "XamlCTask" task failed unexpectedly.
System.ArgumentException: An item with the same key has already been added. Key: Xamarin.Forms.Xaml.XmlName

When I remove one of the Labels it actually works, e.g:

        <StackLayout BindableLayout.ItemsSource="{Binding Spielplan}">
            <BindableLayout.ItemTemplate>
                <DataTemplate>
                    <Label Text="{Binding Datetime}" HorizontalOptions="FillAndExpand" />
               </DataTemplate>
            </BindableLayout.ItemTemplate>
        </StackLayout>

The Spielplan actually contains both (Datetime and Heim), doesn't matter which one I choose for the second example.
I don't really see what I'm doing wrong in the first snippet, any suggestions?

Best Answers

  • NMackayNMackay GB mod
    edited February 27 Accepted Answer

    @fsulser said:
    Having the following problem. When I put in my xaml:

            <StackLayout BindableLayout.ItemsSource="{Binding Spielplan}">
                <BindableLayout.ItemTemplate>
                    <DataTemplate>
                        <Label Text="{Binding Datetime}" HorizontalOptions="FillAndExpand" />
                        <Label Text="{Binding Heim}" HorizontalOptions="FillAndExpand" />
                   </DataTemplate>
                </BindableLayout.ItemTemplate>
            </StackLayout>
    

    I get this error during compilation:
    Error MSB4018: The "XamlCTask" task failed unexpectedly.
    System.ArgumentException: An item with the same key has already been added. Key: Xamarin.Forms.Xaml.XmlName

    When I remove one of the Labels it actually works, e.g:

            <StackLayout BindableLayout.ItemsSource="{Binding Spielplan}">
                <BindableLayout.ItemTemplate>
                    <DataTemplate>
                        <Label Text="{Binding Datetime}" HorizontalOptions="FillAndExpand" />
                   </DataTemplate>
                </BindableLayout.ItemTemplate>
            </StackLayout>
    

    The Spielplan actually contains both (Datetime and Heim), doesn't matter which one I choose for the second example.
    I don't really see what I'm doing wrong in the first snippet, any suggestions?

    You need your data template to have a layout control that can handle multiple child elements

    <StackLayout BindableLayout.ItemsSource="{Binding Spielplan}">
        <BindableLayout.ItemTemplate>
            <DataTemplate>
                <StackLayout>
                    <Label Text="{Binding Datetime}" HorizontalOptions="FillAndExpand" />
                    <Label Text="{Binding Heim}" HorizontalOptions="FillAndExpand" />
                </StackLayout>
            </DataTemplate>
        </BindableLayout.ItemTemplate>
    </StackLayout>
    

Answers

  • NMackayNMackay GBInsider, University mod
    edited February 27 Accepted Answer

    @fsulser said:
    Having the following problem. When I put in my xaml:

            <StackLayout BindableLayout.ItemsSource="{Binding Spielplan}">
                <BindableLayout.ItemTemplate>
                    <DataTemplate>
                        <Label Text="{Binding Datetime}" HorizontalOptions="FillAndExpand" />
                        <Label Text="{Binding Heim}" HorizontalOptions="FillAndExpand" />
                   </DataTemplate>
                </BindableLayout.ItemTemplate>
            </StackLayout>
    

    I get this error during compilation:
    Error MSB4018: The "XamlCTask" task failed unexpectedly.
    System.ArgumentException: An item with the same key has already been added. Key: Xamarin.Forms.Xaml.XmlName

    When I remove one of the Labels it actually works, e.g:

            <StackLayout BindableLayout.ItemsSource="{Binding Spielplan}">
                <BindableLayout.ItemTemplate>
                    <DataTemplate>
                        <Label Text="{Binding Datetime}" HorizontalOptions="FillAndExpand" />
                   </DataTemplate>
                </BindableLayout.ItemTemplate>
            </StackLayout>
    

    The Spielplan actually contains both (Datetime and Heim), doesn't matter which one I choose for the second example.
    I don't really see what I'm doing wrong in the first snippet, any suggestions?

    You need your data template to have a layout control that can handle multiple child elements

    <StackLayout BindableLayout.ItemsSource="{Binding Spielplan}">
        <BindableLayout.ItemTemplate>
            <DataTemplate>
                <StackLayout>
                    <Label Text="{Binding Datetime}" HorizontalOptions="FillAndExpand" />
                    <Label Text="{Binding Heim}" HorizontalOptions="FillAndExpand" />
                </StackLayout>
            </DataTemplate>
        </BindableLayout.ItemTemplate>
    </StackLayout>
    
Sign In or Register to comment.