Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

The property 'Content' is set more than once

I started a new project, and I'm getting the error 'The property 'Content' is set more than once' when I do various edits in a XAML file. Even copying an old XAML file I had that works fine, I get the message.

Here is an example of where I get the message:

<!-- Activity Indicator--> <ActivityIndicator VerticalOptions="Center" HorizontalOptions="Center" x:Name="activity_indicator" Color="#4D7EE1" /> <!-- ListView--> <ListView x:Name="myList" HasUnevenRows="true"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <!-- Here we make a Horizontal orientation with the help of StackLayout--> <StackLayout Orientation="Horizontal" Margin="5" HeightRequest="90"> <Image Source="{Binding image}" WidthRequest="100" HeightRequest="200" Aspect="AspectFit" /> <StackLayout VerticalOptions="Center"> <Label Text="{Binding title}" TextColor="#1C5AD8" /> </StackLayout> <Image HorizontalOptions="EndAndExpand" HeightRequest="20" WidthRequest="20" Source="more_icon.png" /> </StackLayout> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView>

Answers

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    I would say that most likely all of those views you just posted are not in the Xaml file correctly.

        <!-- Activity Indicator-->
        <ActivityIndicator VerticalOptions="Center" HorizontalOptions="Center" x:Name="activity_indicator" Color="#4D7EE1" />
        <ListView x:Name="myList" HasUnevenRows="true">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <!-- Here we make a Horizontal orientation with the help of StackLayout-->
                        <StackLayout Orientation="Horizontal" Margin="5" HeightRequest="90">
                            <Image Source="{Binding image}" WidthRequest="100" HeightRequest="200" Aspect="AspectFit" />
                            <StackLayout VerticalOptions="Center">
                                <Label Text="{Binding title}" TextColor="#1C5AD8" />
                            </StackLayout>
                            <Image HorizontalOptions="EndAndExpand" HeightRequest="20" WidthRequest="20" Source="more_icon.png" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    

    If these views are the top level of a ContentPage, like

    <ContentPage>
        <!-- Activity Indicator-->
        <ActivityIndicator VerticalOptions="Center" HorizontalOptions="Center" x:Name="activity_indicator" Color="#4D7EE1" />
        <ListView x:Name="myList" HasUnevenRows="true">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <!-- Here we make a Horizontal orientation with the help of StackLayout-->
                        <StackLayout Orientation="Horizontal" Margin="5" HeightRequest="90">
                            <Image Source="{Binding image}" WidthRequest="100" HeightRequest="200" Aspect="AspectFit" />
                            <StackLayout VerticalOptions="Center">
                                <Label Text="{Binding title}" TextColor="#1C5AD8" />
                            </StackLayout>
                            <Image HorizontalOptions="EndAndExpand" HeightRequest="20" WidthRequest="20" Source="more_icon.png" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage>
    

    You can see why it would be confused. These can't both be at the Top level, or they are both trying to set the content property of the ContentPage.

    More likely, you want it formatted more like

    <ContentPage>
        <StackLayout>
        <!-- Activity Indicator-->
        <ActivityIndicator VerticalOptions="Center" HorizontalOptions="Center" x:Name="activity_indicator" Color="#4D7EE1" />
        <ListView x:Name="myList" HasUnevenRows="true">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <!-- Here we make a Horizontal orientation with the help of StackLayout-->
                        <StackLayout Orientation="Horizontal" Margin="5" HeightRequest="90">
                            <Image Source="{Binding image}" WidthRequest="100" HeightRequest="200" Aspect="AspectFit" />
                            <StackLayout VerticalOptions="Center">
                                <Label Text="{Binding title}" TextColor="#1C5AD8" />
                            </StackLayout>
                            <Image HorizontalOptions="EndAndExpand" HeightRequest="20" WidthRequest="20" Source="more_icon.png" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        </StackLayout>
    </ContentPage>
    
  • igorkr_10igorkr_10 Member ✭✭✭✭
    edited November 2019

    You need to set Content like this:

    <ContentPage>
        <StackLayout>
            <Label/>
            <Label/>
        </StackLayout>
    </ContentPage>
    

    But better to set content to property Content directly:

    <ContentPage>
        <ContentPage.Content>
            <StackLayout>
                <Label/>
                <Label/>
            </StackLayout>
        </ContentPage.Content>
    </ContentPage>
    

    I think you set Content without layout like this (wrong way):

    <ContentPage>
        <ContentPage.Content>
                <Label/>
                <Label/>
        </ContentPage.Content>
    </ContentPage>
    
  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    @igorkr_10 said:

    But better to set content to property Content directly:

    <ContentPage>
      <ContentPage.Content>
          <StackLayout>
              <Label/>
              <Label/>
          </StackLayout>
      </ContentPage.Content>
    </ContentPage>
    

    Better here is very subjective. I would reject that code from our apps on account of having extra lines that add nothing to the picture.

    For learning, it helps you understand what you are setting, so it might be *better* for learning.

Sign In or Register to comment.