Search code examples
xamlxamarin.formsresourcedictionary

Stand alone ResourceDictionary resource not found


I followed https://learn.microsoft.com/en-us/xamarin/xamarin-forms/xaml/resource-dictionaries#stand-alone-resource-dictionaries but i get the error.

XFC0124 Resource "ResourceDictionary1.xaml" not found.

my code is

ResourceDictionary1.xaml (code-behind deleted) compile option: embeded resource

<?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:String x:Key="test">
        test string
    </x:String>

</ResourceDictionary>

ContentPage1.xaml

   <ContentPage.Resources>
        <ResourceDictionary Source="ResourceDictionary1.xaml"/>
  </ContentPage.Resources>

Solution

  • I test with the steps provided in the link , everything works fine .

    Check the steps below

    1. Add a ContentView file and rename it ResourceDictionary1 .

      enter image description here

    2. Delete code-behind

      enter image description here

    3. Remove x:Class="FormsApp.ResourceDictionary1" in the xaml and change the tag from ContentView to ResourceDictionary .

    <?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:String x:Key="test">
            test string
        </x:String>
    </ResourceDictionary>
    
    1. Usage in other xaml
    
        <ContentPage.Resources>
            <ResourceDictionary Source="ResourceDictionary1.xaml"/>
        </ContentPage.Resources>
        
        
        <ContentPage.Content>
            <StackLayout>
                <Button Clicked="Button_Clicked" Text="{StaticResource test}"/>
            </StackLayout>
        </ContentPage.Content>
    

    enter image description here


    If you have done all the steps above and problem persists , it must be something wrong with the file path .

    The ResourceDictionary file should locate at the same level with the page which using the ResourceDictionary.

    • We can place them at the root of the forms project.

      enter image description here

    • We can place them in same folder .

      enter image description here

    • If they are not in same level , we should set the relative file path in the page .

      enter image description here

       // in page4.xaml
        <ContentPage.Resources>
            <ResourceDictionary Source="MyFolder/ResourceDictionary1.xaml"/>
        </ContentPage.Resources>