2

I have the following style defined in my Content Page:

    <ContentPage.Resources>
        <ResourceDictionary>
            <Style TargetType="{x:Type ContentView}" x:Key="ColorScaleGordura">
                <Setter Property="Content">
                    <Setter.Value>
                        <StackLayout Orientation="Horizontal" Padding="15,0,15,0" HorizontalOptions="Center" Spacing="15" HeightRequest="30">
                            <Frame BackgroundColor="{StaticResource ColorScaleRed}" Style="{StaticResource NutrientColorFrameStyle}">
                                <Label Text="G" Style="{StaticResource NutrientInitialsStyle}"/>
                            </Frame>
                            <Frame BackgroundColor="{StaticResource ColorScaleDarkOrange}" Style="{StaticResource NutrientColorFrameStyle}">
                                <Label Text="G" Style="{StaticResource NutrientInitialsStyle}"/>
                            </Frame>
                            <Frame BackgroundColor="{StaticResource ColorScaleOrange}" Style="{StaticResource NutrientColorFrameStyle}">
                                <Label Text="G" Style="{StaticResource NutrientInitialsStyle}"/>
                            </Frame>
                            <Frame BackgroundColor="{StaticResource ColorScaleYellow}" Style="{StaticResource NutrientColorFrameStyle}">
                                <Label Text="G" Style="{StaticResource NutrientInitialsStyle}"/>
                            </Frame>
                            <Frame BackgroundColor="{StaticResource ColorScaleLightGreen}" Style="{StaticResource NutrientColorFrameStyle}">
                                <Label Text="G" Style="{StaticResource NutrientInitialsStyle}"/>
                            </Frame>
                            <Frame BackgroundColor="{StaticResource ColorScaleGreen}" Style="{StaticResource NutrientColorFrameStyle}">
                                <Label Text="G" Style="{StaticResource NutrientInitialsStyle}"/>
                            </Frame>
                            <Frame BackgroundColor="{StaticResource ColorScaleDarkGreen}" Style="{StaticResource NutrientColorFrameStyle}">
                                <Label Text="G" Style="{StaticResource NutrientInitialsStyle}"/>
                            </Frame>
                        </StackLayout>
                    </Setter.Value>
                </Setter>
            </Style>
        </ResourceDictionary>
    </ContentPage.Resources>

This is what it is supose to look like:

enter image description here

I've made this as a Style because I need it to repeat a lot of times through the page. I am using it like this:

<ContentView Style="{StaticResource ColorScaleGordura}"/>

And it works! But only once. No matter where in my page I paste the code line above, only one will work (usually the one further down the page). If I do something like this:

<StackLayout>
    <Label Text="test1"/>
    <ContentView Style="{StaticResource ColorScaleGordura}"/>
    <StackLayout>
        <Label Text="test2"/>
        <ContentView Style="{StaticResource ColorScaleGordura}"/>
        <StackLayout>
            <Label Text="test3"/>
            <ContentView Style="{StaticResource ColorScaleGordura}"/>
        </StackLayout>
    </StackLayout>
</StackLayout>

This is what I get:

enter image description here

What am I missing?

Nathalia Soragge
  • 1,415
  • 6
  • 21

1 Answers1

0

The issue is, you're trying to add the same Content to more than one ContentView. Elements (present in Style) cannot be added to more than one Logical Parents. To avoid this, you can set x:Shared="False" to your Style.

But i believe x:Shared only works in WPF application. I don't think there is other alternate for Xamarin.Forms. If your intention is repeat the StackLayout, you can try using RepeaterView with DataTemplate to achieve the expected.

There is a workaround available to do this, but the CreateContent() method seems to be unavailable. Still, you can make it working if above solution doesn't work for you.

dhilmathy
  • 2,800
  • 2
  • 21
  • 29