Using ResourceDictionary from another assembly compile error

I'm trying to use ResourceDictionary from another assembly in App.xaml like this:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/UIServiceLib;component/ResourceDictionaries/CommonDictionary.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

and I'm getting compile error:

Invalid URI value: The specified port is invalid.

Rebuilding/cleaning solution didn't help. How can I solve this issue?

Best Answers

  • LandLuLandLu Xamurai
    Accepted Answer

    @ExLuzZziVo If you don't want to use this property. We can also achieve that like:

    <Application xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="Demo.App"
                 xmlns:theme="clr-namespace:UIServiceLib.ResourceDictionaries;assembly=UIServiceLib">
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <theme:CommonDictionary/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    </Application>
    

    This works fine on my side, you can have a try.

  • ExLuzZziVoExLuzZziVo ✭✭
    Accepted Answer

    @LandLu I have tried it too. No result:(
    But then I have found the main reason of such behaviour. The code behind of CommonDictionary was:

    public partial class CommonDictionary
    {
    
    }
    

    But it should be:

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

    Now it works great. Thanks!

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    What you post is not the Xamarin's syntax. Add a new namespace declaration for the library firstly. Then use it with MergedWith. Here is my App.xaml for you referring to:

    <Application xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="Demo.App"
                 xmlns:theme="clr-namespace:UIServiceLib.ResourceDictionaries;assembly=UIServiceLib">
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary MergedWith="theme:CommonDictionary"/>
            </ResourceDictionary>
        </Application.Resources>
    </Application>
    

    The UIServiceLib is your assembly's name, and the ResourceDictionaries is the folder's name. Moreover don't forget to reference this library to your Forms project.

  • ExLuzZziVoExLuzZziVo Member ✭✭

    @LandLu MergedWith is obsolete property, so it doesn't work.

  • LandLuLandLu Member, Xamarin Team Xamurai
    Accepted Answer

    @ExLuzZziVo If you don't want to use this property. We can also achieve that like:

    <Application xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="Demo.App"
                 xmlns:theme="clr-namespace:UIServiceLib.ResourceDictionaries;assembly=UIServiceLib">
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <theme:CommonDictionary/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    </Application>
    

    This works fine on my side, you can have a try.

  • ExLuzZziVoExLuzZziVo Member ✭✭
    Accepted Answer

    @LandLu I have tried it too. No result:(
    But then I have found the main reason of such behaviour. The code behind of CommonDictionary was:

    public partial class CommonDictionary
    {
    
    }
    

    But it should be:

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

    Now it works great. Thanks!

  • colinedwardscolinedwards USMember ✭✭

    doesn't work for me either.
    @ExLuzZziVo, looks like the error you're getting is related to the treatment of the string.

    pack://application:,,,/UIServiceLib;component/ResourceDictionaries/CommonDictionary.xaml

    The packing syntax contains double colons which would indicate a port in a 'proper' URL.
    I'm not sure Xamarin is that clever that it can understand packing syntax; but the other methods suggested are definitely deprecated. Examples are out of date, no real documentation to support current methods. It all seems very half baked to me...

  • colinedwardscolinedwards USMember ✭✭

    update...
    looking into it I can now do this:

    So it seems as long as the XAML is not in the root of the library it will load. I'd consider that a bug.

    As for loading "remote" resource dictionaries, I'm having no joy at all... I've even reverted to applying backing code and used "merge with"; but when I try and run a dynamic resource reference to a binding property on a control in a page the key for the remote resource item is never found...

    I'm running this 'voyage of discovery' on xamarin forms 4 (preview 8); so nothing is improving here...

  • colinedwardscolinedwards USMember ✭✭

    it stripped out the important bit...
    <ResourceDictionary
    Source="/Resources/Icons.xaml" />

  • ExLuzZziVoExLuzZziVo Member ✭✭

    @colinedwards , it's still working for me. You should implement dictionary merging like in these two answers
    1.

    @LandLu said:
    @ExLuzZziVo If you don't want to use this property. We can also achieve that like:

    <Application xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="Demo.App"
                 xmlns:theme="clr-namespace:UIServiceLib.ResourceDictionaries;assembly=UIServiceLib">
      <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <theme:CommonDictionary/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
      </Application.Resources>
    </Application>
    

    This works fine on my side, you can have a try.

    2.

    @ExLuzZziVo said:
    @LandLu I have tried it too. No result:(
    But then I have found the main reason of such behaviour. The code behind of CommonDictionary was:

    public partial class CommonDictionary
    {

    }

    But it should be:

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

    Now it works great. Thanks!

  • colinedwardscolinedwards USMember ✭✭

    you I think I missed the "initialise component"; I'll give it another go. thanks :)

Sign In or Register to comment.