A resource with the key 'Xamarin.Forms.StackLayout' is already present in the ResourceDictionary

Hello everyone, I am getting the following Exception:
System.ArgumentException: A resource with the key 'Xamarin.Forms.StackLayout' is already present in the ResourceDictionary.

Because of this code in my App.xaml ResourceDictionary:

<Application.Resources>
        <ResourceDictionary>
            <!-- omitted irrelevant parts -->

            <Style TargetType="StackLayout" x:Key="VerticalStack">
                <Setter Property="Orientation" Value="Vertical"/>
                <Setter Property="Spacing" Value="16"/>
            </Style>

            <Style x:Name="FormFirstPortraitStyle" TargetType="StackLayout" BasedOn="{StaticResource VerticalStack}">
                <Setter Property="HorizontalOptions" Value="Center"/>
                <Setter Property="VerticalOptions" Value="Start"/>
                <Setter Property="Margin" Value="16, 100, 16, 16"/>
            </Style>

            <Style x:Name="FormFirstLandscapeStyle" TargetType="StackLayout" BasedOn="{StaticResource VerticalStack}">
                <Setter Property="HorizontalOptions" Value="Center"/>
                <Setter Property="VerticalOptions" Value="Center"/>
                <Setter Property="Margin" Value="16, 16, 16, 16"/>
            </Style>
        </ResourceDictionary>
    </Application.Resources>

If I remove the "FormFirstLandscapeStyle" style, it works.
I am setting the TargetType to StackLayout, not the key... I think I am doing something wrong, but I cannot figure out what...
Thanks for helping.

Posts

  • JannikRJannikR Member ✭✭
    edited February 4

    Hi,

    you should use the x:Key Property to assign a valid key to your style. The x:Name Attribute isn't needed here.

    So your code should look like:

    <Application.Resources>
            <ResourceDictionary>
                <!-- omitted irrelevant parts -->
    
                <Style TargetType="StackLayout" x:Key="VerticalStack">
                    <Setter Property="Orientation" Value="Vertical"/>
                    <Setter Property="Spacing" Value="16"/>
                </Style>
    
                <Style x:Key="FormFirstPortraitStyle" TargetType="StackLayout" BasedOn="{StaticResource VerticalStack}">
                    <Setter Property="HorizontalOptions" Value="Center"/>
                    <Setter Property="VerticalOptions" Value="Start"/>
                    <Setter Property="Margin" Value="16, 100, 16, 16"/>
                </Style>
    
                <Style x:Key="FormFirstLandscapeStyle" TargetType="StackLayout" BasedOn="{StaticResource VerticalStack}">
                    <Setter Property="HorizontalOptions" Value="Center"/>
                    <Setter Property="VerticalOptions" Value="Center"/>
                    <Setter Property="Margin" Value="16, 16, 16, 16"/>
                </Style>
            </ResourceDictionary>
        </Application.Resources>
    
  • I can't believe it was this :D Thank you.
    It was so obvious, but still, I didn't notice.
    In the past in WPF I've done exactly the same mistake, but WPF gives you a more detailed Exception.

  • JannikRJannikR Member ✭✭

    Sometimes it is that easy :)

    (If it solves your problem, please mark the post as answer. To make it easier for others having the same issue)

  • Salvatore_StrazzulloSalvatore_Strazzullo Member ✭✭
    edited February 4

    Unfortunately, being a newbie, I created the post as a discussion and not as a question, so there is no "Mark as answer" button for me :(

Sign In or Register to comment.