Xamarin.Forms: How can I load ResourceDictionary from another file?

13,542

Solution 1

Merged Dictionaries aren't supported in Xamarin Forms XAML below 2.1.0

The only way you can do it, is to put it in another page and then load it up in code behind and reference it via DynamicResource instead of StaticResource.

I explain this more here: http://www.xamarinhelp.com/styling-uiux-day-11/

However as of 2.1.0-pre1 (released this week), you can now do templating, which is another way to do this. Jason Smith has blogged about it: http://xfcomplete.net/general/2016/01/20/control-templates/

Update: As of 2.3.0 you can do a Merged Dictionary with an attribute called MergedWith.

Create a new XAML File

<?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="UIDemo.Style.NewStyle">
    <Style TargetType="Label">
        <Setter Property="TextColor" Value="Blue" />
    </Style>
</ResourceDictionary>

Now in your ContentPage use the MergedWith attribute on your ResourceDictionary.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:theme="clr-namespace:UIDemo.Style"
             x:Class="UIDemo.MainPage">
    <ContentPage.Resources>
        <ResourceDictionary MergedWith="theme:NewStyle" />
    </ContentPage.Resources>
    <Grid>
        <Label Text="Hello" />
    </Grid>
</ContentPage>

Solution 2

As of 2.3.0 can officially merge Resource dictionaries in xaml observe the following example

BlueTheme.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"
                    x:Class="UI.Themes.BlueTheme">
    <Style TargetType="Label">
        <Setter Property="TextColor" Value="Blue" />
    </Style>
</ResourceDictionary>

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:theme="clr-namespace:UI.Themes"
             x:Class="UI.App">
    <Application.Resources>
        <ResourceDictionary MergedWith="themes:BlueTheme" />
    </Application.Resources>
    <Label Text="Hello" />
</Application>

Solution 3

From Xamarin.Forms 3.0 MergedWith has been deprecated and should not be used.

Now you can use either Source or MergeDictionaries.

MyResource.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"
                    x:Class="UI.MyResource">
    <Style TargetType="Label">
        <Setter Property="TextColor" Value="Blue" />
    </Style>
</ResourceDictionary>

So, if MyResource.xaml is in the same assembly you can use directly:

<?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="UI.AnotherResource"
    Source="MyResource.xaml">
</ResourceDictionary>

Otherwise (MyResource.xaml is on another assembly):

<?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:otherresources="clr-namespace:OtherAssembly.UI.Resources"
    x:Class="UI.AnotherResource">
    <ResourceDictionary.MergedDictionaries>
        <otherresources:MyResource />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

Some things to take into consideration:

  • Source: It can only be used in XAML, and the value is the path to the xaml file (always in same assembly)
  • MergeDictionaries: It can be used regardless of the assembly where the resource reside

More info in Xamarin Forms Resource Dictionaries

Share:
13,542
P3PPP
Author by

P3PPP

Updated on June 19, 2022

Comments

  • P3PPP
    P3PPP about 2 years

    I wrote following code, but XamlParseException has bean thrown. ("StaticResource not found for key CustomColor")

    MyPage.xaml

    <?xml version="1.0" encoding="UTF-8"?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="XFApp11.MyPage">
        <ContentPage.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="CustomResource.xaml" />
                </ResourceDictionary.MergedDictionaries> 
            </ResourceDictionary>
        </ContentPage.Resources>
    
        <ContentPage.Content>
            <BoxView Color="{StaticResource CustomColor}" />
        </ContentPage.Content>
    </ContentPage>
    

    CustomResource.xaml (build action = EmbeddedResource)

    <?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 x:Key="CustomColor">#004B86</Color>
    </ResourceDictionary>
    
  • P3PPP
    P3PPP over 8 years
    Thank you for answer, ResourceDictionary.Source property is also not supported?
  • Adam
    Adam over 8 years
    Not that I am aware of. I vaguely remember looking into it a while back. You can't reference another XAML file for your resource dictionary in XF. But I haven't gone and tested that just now, so if you can, someone please correct me :)
  • Sagar Kulkarni
    Sagar Kulkarni almost 8 years
    What type you used to inherit BlueTheme.xaml. Could you share code BlueTheme.xaml.cs
  • Ahmed Alejo
    Ahmed Alejo almost 8 years
    This should be clear from the Xaml: ResourceDictionary if it had a different base class i´d need to write something similar to local:ResourceDictionary
  • Ahmed Alejo
    Ahmed Alejo almost 8 years
    This should be clear from the Xaml: ResourceDictionary if it had a different a custom base class i´d need to write something similar to custom-namespace:ResourceDictionary Also ResourceDictionary is public as of 2.3.0
  • zquanghoangz
    zquanghoangz over 7 years
    I followed your answers, when I'm debugging I see Resources list, the Count property is including all my resource (my code is 54) but the Keys and Values is not (my code is only 1 key and value). It also runtime error for cannot found resource key XXX. Look like it knows merged resource but it's not applied
  • zquanghoangz
    zquanghoangz over 7 years
    My Xamarin Forms version is 2.3.2.127
  • zquanghoangz
    zquanghoangz over 7 years
    I do exactly the same, it updates the count of Resoures but it doesn't update the Keys and Values. That make runtime error.
  • Emil
    Emil over 7 years
    @AdamPedley can I create multiple resource dictionary files and merge them in App.xaml?
  • Adam
    Adam over 7 years
    @batmaci - not that I know of, it is one of the limitations that there is only one MergedWith property and it only accepts one style.
  • Adam
    Adam over 7 years
    @batmaci - though its something I have always wanted as well, so I submitted an API request to Xamarin for it. forums.xamarin.com/discussion/86308/…
  • Manikandan
    Manikandan about 7 years
    @AhmedAlejo, I need to define my resources in non embedded file. Is there way to merge resource dictionary from asset files? I mean loading resource dictionary at run time.
  • Ahmed Alejo
    Ahmed Alejo about 7 years
    @Manikandan Xamarin.Forms.Xaml.LoadFromXaml<TXaml>(this TXaml view, string xaml) is public as of Xamarin.Forms.2.3.2.127
  • Sergey
    Sergey about 7 years
    don't forget to call InitializeComponent() in the constructor of your UI.Themes.BlueTheme class. found working sample here - xamarinhelp.com/merged-dictionaries-xamarin-forms
  • Emil
    Emil about 4 years
    everybody is writing different way and xamarin is keep changing such basic thing. your sample is also outdated now
  • fmaccaroni
    fmaccaroni about 4 years
    why do you think it's updated? the official docs still says to use Source and MergeDictionaries. What new thing are you referring to @batmaci? And btw, if you are going to make a comment like that, please provide useful information instead of just criticise the technology
  • Emil
    Emil about 4 years
    it is just big confusing the documentation of xamarin. I thought first Source property is enhancement and better version of merged dictionaries. they wrote as if that you should use source within the same assembly and mergeddictionaries when another assembly. Obviously both can be used and i believe that they have different way of execution. One is creating instance and another doesnt everytime page resources are loaded.