Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How do I reference one XAML MergedDictionary from another?

EasyGoingPatEasyGoingPat GBMember ✭✭✭

I am struggling with XAML Merged Dictionaries. Is it possible to get something like the following working?

File Colours.xaml contains this:

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

    <Color x:Key="WarningTextColour">#FF0000</Color>

</ResourceDictionary>

Another file Text.xaml contains something like this:

<?xml version="1.0" encoding="utf-8" ?>
<?xaml-comp compile="true" ?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <Style x:Key="WarningTextStyle" TargetType="Label">
        <Setter Property="TextColor" Value="{StaticResource WarningTextColour}"/>
        <Setter Property="FontSize" Value="Large"/>
    </Style>
</ResourceDictionary>

Then somewhere like my main App.xaml file, I bring them together like this:

<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="myProject.Forms.App">

    <Application.Resources>
        <ResourceDictionary Source="Styles/Colours.xaml"/>
        <ResourceDictionary Source="Styles/Text.xaml"/>
    </Application.Resources>

</Application>

I've read the guides and tried various combinations of syntax, trying both the old and the new ways, but I can't find a way to do this. I am beginning to wonder if I am maybe approaching this completely the wrong way.

Even with all my XAML set to compile, the project builds okay (which is disappointing if I have something wrong), but the MainPage.xaml that tries to use the WarningTextStyle gives an exception.

Any help or advice would be very much appreciated.

Kind wishes ~ Patrick

Best Answers

  • EasyGoingPatEasyGoingPat GBMember ✭✭✭
    Accepted Answer

    @NMackay
    @tsgriggs2
    @AbhijitDeshpande

    This is a link to a small demo of how I set up my Xamarin MergedDictionaries. Hope it helps.
    https://1drv.ms/u/s!AjTIcJqQTWAFknJRxN97kpfk3gOR

    • Patrick
  • MartinBrekhofMartinBrekhof NLUniversity ✭✭
    edited November 2018 Accepted Answer

    Can be done:
    1. add the file with styles to your current project as a linked file
    2. make sure it has Build action (properties) defined as EmbeddedResource

    I added it to the root of my project (where app.xaml lives).

    hth
    Martin

Answers

  • AbhijitDeshpandeAbhijitDeshpande INMember ✭✭

    I have an extended question, can we use the "Pure XAML ResourceDictionaries" (as mentioned in blog https://blog.xamarin.com/better-resource-organization-xamarin-forms/)

    If yes, what build action to specify when you don't have associated .cs file ?

    I just created the text file with extension .xaml (added the tag <?xaml-comp compile="true" ?>)
    but getting an compile error

  • EasyGoingPatEasyGoingPat GBMember ✭✭✭

    @AbhijitDeshpande

    In my example above, none of my resource dictionaries have a .cs file except, of course, the top-level Application dictionary. By setting the xaml-comp compile='true', as you say, it all works fine. What exactly is your error?

  • AbhijitDeshpandeAbhijitDeshpande INMember ✭✭

    Thanks @EasyGoingPat.

    This is pretty embarrassing. I did the steps again and its working as expected.
    May be needed your blessings.

    Cheers

  • tsgriggs2tsgriggs2 Member ✭✭
    edited September 2018

    I am struggling with this solution. For some reason, I get the following error, when building:

    "Position 7:25. Resource 'Resources/Styles/ColorStyles.xaml' not found."

    This is a new account, so I had to break the xmlns links in my code, below...

    My code is:

    ColorStyles.xaml

    <?xml version="1.0" encoding="utf-8" ?>
    <?xaml-comp compile="true" ?>
    <ResourceDictionary xmlns="http: //xamarin.com/schemas/2014/forms"
                        xmlns:x="http: //schemas.microsoft.com/winfx/2009/xaml">
    
        <!-- Base Colors -->
        <Color x:Key="Black">#000000</Color>
        <Color x:Key="Beige">#FFF5F5DC</Color>
        <Color x:Key="Gray">#808080</Color>
        <Color x:Key="LightGray">#D9D9D9</Color>
        <Color x:Key="Transparent">#00000000</Color>
        <Color x:Key="White">#FFFFFF</Color>
    
    </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"
                 x:Class="iAssess.App">
    
      <Application.Resources>
        <ResourceDictionary Source="Resources/Styles/ColorStyles.xaml"/>
      </Application.Resources>
    
    </Application>
    

    App.xaml is on the same level as my Resources directory. ColorStyles.xaml is located in Resources/Styles.

    Any idea why I may be experiencing this? I have been stuck on it for hours and have never seen this issue before.

  • EasyGoingPatEasyGoingPat GBMember ✭✭✭

    @tsgriggs2

    Does the code you give above give you an error or do you get an error when you try to use the styles in a normal XAML page?

    (I'm going to struggle to help until next Wednesday because I am away from my development machine until then.)

    • Patrick
  • tsgriggs2tsgriggs2 Member ✭✭

    @EasyGoingPat

    Thank you for the quick response! I have not been able to get the ResourceDictionary to work, from any XAML pages. If I add the color elements to the App.xaml Resources, I don't get any build errors. I would really like to use the merged dictionary functionality, so that we can keep the styles in separate files (for organization sake).

    To give some background, I am working on a cross-platform app that handles all the UI stuff programatically, and we're starting to move the UI layer to XAML. I have never seen this problem, but I have never done this stuff from scratch before. Our NuGet Packages show we're using Xamarin.Forms v3.2.0.839982 (latest), which should allow to use the shorthand merged dictionaries, correct?

  • EasyGoingPatEasyGoingPat GBMember ✭✭✭

    @tsgriggs2

    The version of Xamarin you are using should allow for Merged Dictionaries, yes.

    Are you still having problems? If you are, I may be able to send you a link to some working code that shows how I have set mine up. It may help you to work out what is wrong with your implementation.

    ~ Patrick

  • NMackayNMackay GBInsider, University admin

    @tsgriggs2 said:
    @EasyGoingPat

    Thank you for the quick response! I have not been able to get the ResourceDictionary to work, from any XAML pages. If I add the color elements to the App.xaml Resources, I don't get any build errors. I would really like to use the merged dictionary functionality, so that we can keep the styles in separate files (for organization sake).

    To give some background, I am working on a cross-platform app that handles all the UI stuff programatically, and we're starting to move the UI layer to XAML. I have never seen this problem, but I have never done this stuff from scratch before. Our NuGet Packages show we're using Xamarin.Forms v3.2.0.839982 (latest), which should allow to use the shorthand merged dictionaries, correct?

    This approach definitely works in 3.2.0, I was doing this last week. Why not create a little repo project and post as an attachment so we can take a look.

  • EasyGoingPatEasyGoingPat GBMember ✭✭✭
    Accepted Answer

    @NMackay
    @tsgriggs2
    @AbhijitDeshpande

    This is a link to a small demo of how I set up my Xamarin MergedDictionaries. Hope it helps.
    https://1drv.ms/u/s!AjTIcJqQTWAFknJRxN97kpfk3gOR

    • Patrick
  • tsgriggs2tsgriggs2 Member ✭✭
    edited October 2018

    @EasyGoingPat
    @NMackay
    Thanks again! There didn't seem to be any reason that the dictionaries wouldn't work, so I decided that the best approach was for me to start with a new project and migrate the code piece by piece. The dictionaries seem to be working in the new project, so I think I am good. I appreciate the help, and I will post again, if I run into more trouble!

  • UnreachableCodeUnreachableCode USMember ✭✭✭

    Excellent question and answer, @EasyGoingPat. Does anyone know how you might set the source to a path in a different project, in this line:

    <ResourceDictionary Source="Styles/Colours.xaml"/>

    Thanks!

  • EasyGoingPatEasyGoingPat GBMember ✭✭✭

    @CharlieFinlayson.3926

    Mmm... good question. You probably need something like this WPF feature: https://docs.microsoft.com/en-us/dotnet/framework/wpf/app-development/pack-uris-in-wpf

    Not sure if Xamarin supports anything like that.

    ~ Patrick

  • UnreachableCodeUnreachableCode USMember ✭✭✭

    Thanks Patrick. That's a good shout. I'll let you know how I get on.

  • MartinBrekhofMartinBrekhof NLUniversity ✭✭

    @CharlieFinlayson.3926 said:
    Excellent question and answer, @EasyGoingPat. Does anyone know how you might set the source to a path in a different project, in this line:

    <ResourceDictionary Source="Styles/Colours.xaml"/>

    Thanks!

    I got it (finally) compiling and running using the following syntax, note the forward slash :

  • EasyGoingPatEasyGoingPat GBMember ✭✭✭

    @MartinBrekhof said:

    <ResourceDictionary Source="Styles/Colours.xaml"/>

    So that accesses XAML styles in a different project?

  • MartinBrekhofMartinBrekhof NLUniversity ✭✭
    edited November 2018 Accepted Answer

    Can be done:
    1. add the file with styles to your current project as a linked file
    2. make sure it has Build action (properties) defined as EmbeddedResource

    I added it to the root of my project (where app.xaml lives).

    hth
    Martin

  • batmacibatmaci DEMember ✭✭✭✭✭

    @EasyGoingPat I was pulling my hair and finally downloaded your sample project and compared with my one. Only difference was <?xaml-comp compile="true" ?> tags on top. What the hack is that? why it works with it and crashes without? Should we use it in every resource file or only for those referencing inside a another resource file? So it works like a partial class i guess?
    i found this issue report on github https://github.com/xamarin/Xamarin.Forms/issues/3655

  • EasyGoingPatEasyGoingPat GBMember ✭✭✭

    @batmaci

    Glad my example helped!

    Personally, I tend to use <?xaml-comp compile="true"?> in every XAML file. It doesn't do anything worrying; it just compiles the XAML to BAML at build-time. So far I haven't found any reason why you would want anything different, and (though I may stand corrected) I don't see why it is not the default behaviour. Surely, the only reason you might turn it off is if you had a long build-time, and even then I'm not sure how much improvement you'd get.

    Anyway, glad you got it working.

    • Patrick
  • batmacibatmaci DEMember ✭✭✭✭✭

    Thanks for your comment. it makes sense but i totaly reverted my changes from Source property to mergeddictionary style. Source property makes a lot of sense and keeps xaml clean. This is how UWP and WPF used to work but there is again implementation mistakes or incomplete feature from Xamarin team. I found 2 showstopper issues.
    1. if you use full linker, i cant find a way to preserve those resource files. Here is my question.
    2. If you use source property and if you have dynamic theme update, resources linked with source property wont update. here is that SO post .

    therefor i converted all my resource back to with cs code behind and using mergeddictionary style.

  • EasyGoingPatEasyGoingPat GBMember ✭✭✭

    I've actually started doing all my layout in C#, using the excellent https://github.com/VincentH-Net/CSharpForMarkup. After a couple of hours, it feels natural and simplifies so many things. A couple of things get a touch more tricky (you need to pay money to get something to do hot reload) but - on balance - I feel that XAML was only slowing me down. Unless you are a big organisation, in which you really do benefit from the separation of concerns by having specialist UI guys who don't know how to write code, I don't feel XAML brings much to the party.

Sign In or Register to comment.