Xamarin.Forms App.xaml MergedDictionaries - StaticResource Lookup Not Detecting Styles

Viewed 229

I am creating an application which will later go to the Play Store. The application is well-organised and tidy - and utilises the MVVM pattern.

In my application - I have a folder titled 'Styles' which contains numerous styles for different elements of my application:

enter image description here

As you can see I have a ResourceDictionary for a floating action button:

<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                    xmlns:FAB="clr-namespace:Refractored.FabControl;assembly=Refractored.FabControl"
                    xmlns:rsvpapp="clr-namespace:RSVPApp"
                    x:Class="RSVPApp.Styles.FloatingActionButton">

    <Color x:Key="FAB_BlueNormalStyleColor">
        #0BA1D8
    </Color>

    <Color x:Key="FAB_BlueRippleStyleColor">
        #21B7ED
    </Color>

    <Color x:Key="FAB_BluePressedStyleColor">
        #3DCBFF
    </Color>


    <Color x:Key="FAB_RedNormalStyleColor">
        #CF0000
    </Color>

    <Color x:Key="FAB_RedRippleStyleColor">
        #ED2121
    </Color>

    <Color x:Key="FAB_RedPressedStyleColor">
        #FF3D3D
    </Color>

    
    <Color x:Key="FAB_OrangeNormalStyleColor">
        #FFA500
    </Color>

    <Color x:Key="FAB_OrangeRippleStyleColor">
        #EDA621
    </Color>

    <Color x:Key="FAB_OrangePressedStyleColor">
        #FFBB3D
    </Color>


    <Color x:Key="FAB_GreenNormalStyleColor">
        #00E500
    </Color>

    <Color x:Key="FAB_GreenRippleStyleColor">
        #69FA69
    </Color>

    <Color x:Key="FAB_GreenPressedStyleColor">
        #85FF85
    </Color>

    <!--Blue-->
    <Style x:Key="FAB_BlueRegularStyle"
           TargetType="FAB:FloatingActionButtonView">
        <Setter Property="ImageName"
                Value="ic_add_white.png" />

        <Setter Property="ColorNormal"
                Value="{StaticResource FAB_BlueNormalStyleColor}" />

        <Setter Property="ColorRipple"
                Value="{StaticResource FAB_BlueRippleStyleColor}" />

        <Setter Property="ColorPressed"
                Value="{StaticResource FAB_BluePressedStyleColor}" />

        <Setter Property="HasShadow"
                Value="True" />

        <Setter Property="IsVisible"
                Value="True" />
    </Style>

    <Style x:Key="FAB_BlueMiniStyle"
           TargetType="FAB:FloatingActionButtonView"
           BasedOn="{StaticResource FAB_BlueRegularStyle}">
        <Setter Property="Size"
                Value="Mini" />
    </Style>

    
    <!--Red-->
    <Style x:Key="FAB_RedRegularStyle"
           TargetType="FAB:FloatingActionButtonView"
           BasedOn="{StaticResource FAB_BlueRegularStyle}">
        <Setter Property="ColorNormal"
                Value="{StaticResource FAB_RedNormalStyleColor}" />

        <Setter Property="ColorRipple"
                Value="{StaticResource FAB_RedRippleStyleColor}" />

        <Setter Property="ColorPressed"
                Value="{StaticResource FAB_RedPressedStyleColor}" />
    </Style>

    <Style x:Key="FAB_RedMiniStyle"
           TargetType="FAB:FloatingActionButtonView"
           BasedOn="{StaticResource FAB_RedRegularStyle}">
        <Setter Property="Size"
                Value="Mini" />
    </Style>

    
    <!--Orange-->
    <Style x:Key="FAB_OrangeRegularStyle"
           TargetType="FAB:FloatingActionButtonView"
           BasedOn="{StaticResource FAB_BlueRegularStyle}">
        <Setter Property="ColorNormal"
                Value="{StaticResource FAB_OrangeNormalStyleColor}" />

        <Setter Property="ColorRipple"
                Value="{StaticResource FAB_OrangeRippleStyleColor}" />

        <Setter Property="ColorPressed"
                Value="{StaticResource FAB_OrangePressedStyleColor}" />
    </Style>

    <Style x:Key="FAB_OrangeMiniStyle"
           TargetType="FAB:FloatingActionButtonView"
           BasedOn="{StaticResource FAB_OrangeRegularStyle}">
        <Setter Property="Size"
                Value="Mini" />
    </Style>


    <!--Green-->
    <Style x:Key="FAB_GreenRegularStyle"
           TargetType="FAB:FloatingActionButtonView"
           BasedOn="{StaticResource FAB_BlueRegularStyle}">
        <Setter Property="ColorNormal"
                Value="{StaticResource FAB_GreenNormalStyleColor}" />

        <Setter Property="ColorRipple"
                Value="{StaticResource FAB_GreenRippleStyleColor}" />

        <Setter Property="ColorPressed"
                Value="{StaticResource FAB_GreenPressedStyleColor}" />
    </Style>

    <Style x:Key="FAB_GreenMiniStyle"
           TargetType="FAB:FloatingActionButtonView"
           BasedOn="{StaticResource FAB_GreenRegularStyle}">
        <Setter Property="Size"
                Value="Mini" />
    </Style>
</ResourceDictionary>

Now - I want to merge all of my resource dictionaries into my app.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:styles="clr-namespace:RSVPApp.Styles"
             x:Class="RSVPApp.App">
    <!--
        Define global resources and styles here, that apply to all pages in your app.
    -->
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <styles:FloatingActionButton />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
            <Color x:Key="Primary">#2196F3</Color>
            <Style TargetType="Button">
                <Setter Property="TextColor" Value="White"></Setter>
                <Setter Property="VisualStateManager.VisualStateGroups">
                    <VisualStateGroupList>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal">
                                <VisualState.Setters>
                                    <Setter Property="BackgroundColor" Value="{StaticResource Primary}" />
                                </VisualState.Setters>
                            </VisualState>
                            <VisualState x:Name="Disabled">
                                <VisualState.Setters>
                                    <Setter Property="BackgroundColor" Value="#332196F3" />
                                </VisualState.Setters>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateGroupList>
                </Setter>
            </Style>
        </ResourceDictionary>        
    </Application.Resources>
</Application>
 

As you can see - at the top I have declared a merged dictionaries attribute and inside I have the appropriate resource dictionaries.

In one of my pages - I decided that I would want to test whether the styles are detected:

    <fab:FloatingActionButtonView Style="{StaticResource FAB_BlueRegularStyle}"/>

Whenever I build the application - it immediately crashes because it cannot detect the style.

Maybe I am doing something wrong? I am not entirely sure. I am looking for a solution to this.

Thank you,

3 Answers

I created a test project and tried to resolve your problem. I couldnt find heavy problems. I also commented the line where you specify the image source. Here are my results, I found about your problem;

thoughts

at first, instead of

<Style x:Key="FAB_BlueRegularStyle"
TargetType="FAB:FloatingActionButtonView">
...
</Style>

use this;

<Style x:Key="FAB_BlueRegularStyle"
TargetType="{x:Type FAB:FloatingActionButtonView}">
...
</Style>

You profit from that.

test result

here is my result from the emulator and the compiler. result

potential mistakes

Maybe you declared something wrong in your .csproj file, because in the following picture I cant see an arrow to see the xaml.cs file. In the Styles_FloatingActionButton.xaml file you point to the FloatingActionButton class declaration. Do you really have this file?
this

Maybe recreate your resource dictionary and check your file properties. It should look like this. style

Remove one ResourceDictionary level.

enter image description here

In my project I use CSS files like below. You can then set a lot (not all) properties using CSS.

Here's more information on the subject: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/styles/css/

Here's what it looks like far from finished though:

enter image description here enter image description here enter image description here

This file is called Main.css and it is in a 'css' directory:

* {
    background-color: black;
    color: white;
}

body{
}

.StackLayout
{
    padding: 0;
}

Button {
    background-color: darkslateblue;
    color: white;
    font-size: 10;
    margin: 1;
    border-radius: 5;
}

Button.Delete {
    background-color: red;
    color: white;
}

Editor {
    color: black;
    background-color: white;
    min-height: 300;
}

Entry {
    color: white;
    background-color: black;
}

Label {
    color: white;
    background-color: black;
}

TabbedPage {
    BarBackgroundColor: black;
    color: white;
}

.WordContainer {
}

StackLayout.TitleViewBack {
    background-color: #2296f3;
}

Label.TitleView {
    font-size: 30;
    background-color: #2296f3;
    color: wheat;
}

.ToolbarTitle {
    text-align: center;
    color: red;
}

In my App.xaml I use the following. The stylesheet is now available for all xaml forms.

<?xml version="1.0" encoding="utf-8" ?>
<tgb:TxfApplication
    xmlns:forms="clr-namespace:MvvmCross.Forms.Views;assembly=MvvmCross.Forms"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:tgb="clr-namespace:TGB.Xamarin.Forms;assembly=TGB.Xamarin.Forms"
    xmlns:views="clr-namespace:eTabber.Views"
    xmlns:css="clr-namespace:XamlCSS;assembly=XamlCSS"
    x:Class="eTabber.App">
    <tgb:TxfApplication.Resources>
        <StyleSheet Source="/css/Main.css" />

        <views:TitleView x:Key="MainTitleView"/>

        <Style TargetType="ContentPage" x:Key="TitleView">
            <Setter Property="NavigationPage.TitleView" Value="{StaticResource MainTitleView}"/>
        </Style>
    </tgb:TxfApplication.Resources>
</tgb:TxfApplication>
Related