Stefanija
Stefanija

Reputation: 1418

Resources in Merged Dictionary not loaded on label

I have created Resource Dictionary for Dimens and Colors in project

root: MyProject.Resources.Values /Dimens /Colors

Colors:

<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms" 
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                    x:Class="MyProject.Resources.Values.Colors">
    <!-- Colours Light Theme -->
   
    <Color x:Key="BlackColor">#000000</Color>

</ResourceDictionary>

Dimens:

<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms" 
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                    x:Class="MyProject.Resources.Values.Dimens">

    <x:Double x:Key="FontSize18">18</x:Double>
</ResourceDictionary>

Added them in App class as:

<unity:PrismApplication  xmlns:unity="clr-namespace:Prism.Unity;assembly=Prism.Unity.Forms"
                         xmlns="http://xamarin.com/schemas/2014/forms"
                         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                         xmlns:resources="clr-namespace:MyProject.Resources.Values"
                         x:Class="MyProject.App">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <resources:Dimens />
                <resources:Colors />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</unity:PrismApplication>

And tried to use them on page:

<Label Text="Welcome to Xamarin.Forms!"
                   FontSize="{StaticResource FontSize18}"
                   TextColor="{StaticResource PrimaryColor}"
                   VerticalOptions="CenterAndExpand" 
                   HorizontalOptions="CenterAndExpand" />

But nothing is loaded on the label.

Upvotes: 0

Views: 146

Answers (1)

Leo Zhu
Leo Zhu

Reputation: 14956

You should remove the x:Class attribute from the root tag of the ResourceDictionary files.

Colors:

<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms" 
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                >
     <!-- Colours Light Theme -->

     <Color x:Key="BlackColor">#000000</Color>

</ResourceDictionary>

Dimens:

<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms" 
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                >

    <x:Double x:Key="FontSize18">18</x:Double>
</ResourceDictionary>

then in you app.xaml:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Colors.xaml"></ResourceDictionary>
            <ResourceDictionary Source="Dimens.xaml"></ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Upvotes: 1

Related Questions