Why isn't my ResourceDictionary xaml file being found?

Viewed 1678

I have a Xamarin project with the following structure:

MyApp
 - Converters
 - Models
 - Styles
  -Colors.xaml
 - ViewModels
 - Views
  -ListPage.xaml
   -ListPage.xaml.cs

I am trying to use Colors.xaml as an external Resource Dictionary for my app-wide color scheme. I've added the source to the 'ListPage.xaml' resources like so:

    <ContentPage.Resources>
        <ResourceDictionary Source="Styles/Colors.xaml"/>
        <!--<ResourceDictionary>
            <converter:StatusToColorConverter x:Key="StatusToColorConverter" />
        </ResourceDictionary>-->
    </ContentPage.Resources>

When I try to build the project I get several build errors that seem to start with the error
Resource "Styles/Colors.xaml not found.

I've tried removing the class tag from the Colors.xaml file as well as the code-behind (obviously there isn't one in my structure) file as per this documentation and that still didn't solve the problem. I've also added the same <ResourceDictionary Source="Styles/Colors.xaml"/> to my App file to no avail. What am I missing in order to direct the ListPage.xaml to the Colors.xaml file?

All relevant code below:
Colors.xaml

<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <!--Color Scheme-->
    <Color x:Key="ColorBackground">#333333</Color>
    <Color x:Key="ColorObjectBackground">#FFFFFF</Color>
    <Color x:Key="ColorTierOneText">#3c3c3c</Color>
    <Color x:Key="ColorTierTwoText">#999999</Color>
</ResourceDictionary>   

ListPage.xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:converter="clr-namespace:MyApp"
             mc:Ignorable="d"
             x:Class="MyApp.Views.ListPage" 
             Title="this is the title">

    <ContentPage.BackgroundColor>
        <OnPlatform x:TypeArguments="Color">
            <On Platform="GTK" Value="#3c3c3c" />
        </OnPlatform>
    </ContentPage.BackgroundColor>

    <ContentPage.Resources>
        <ResourceDictionary Source="Styles/Colors.xaml"/>
        <!--<ResourceDictionary>
            <converter:StatusToColorConverter x:Key="StatusToColorConverter" />
        </ResourceDictionary>-->
    </ContentPage.Resources>

    <StackLayout>
        <Label x:Name="NotFoundMessage"
                   IsVisible="False"
                   Text="No results found."
                   HorizontalTextAlignment="Center"
                   TextColor="ColorTierTwoText"
                   Padding="5"/>
    </StackLayout>  

    </ContentPage.Content>
</ContentPage>

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:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="MyApp.App">


    <Application.Resources>
        <ResourceDictionary Source="Styles\Colors.xaml"/>
    </Application.Resources>
</Application>
1 Answers

The Path you have given seems wrong Styles\Colors type of path works only for Android.

To add external file you need to add xmlns reference.

and you are missing x:Class attribute. Just add it like ContentPage and then change Base class. It has to compile for it to work.

Add x:Class="MyApp" to Colors.xaml ResourceDictionary attribute

<?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="MyApp">

Code behind

public partial class Colors : ResourceDictionary
{
    public Colors()
    {
        InitializeComponent();
    }
}

In App.xaml

xmlns:local="clr-namespace:WhereColorsNamespace."

Also you need to add use MergedDictionary

 <ResourceDictionary.MergedDictionaries>
      <local:Colors x:Key="colors"/>
</ResourceDictionary.MergedDictionaries>

Reference : https://xamarinhelp.com/merged-dictionaries-xamarin-forms/

Related