Merging ResourceDictionary from a different Project.

Hello and Thanks for including me in the Xamarin community.

Background

I am performing maintenance on an application using .NETStandard .Library v2.0.3, Xamarin.Forms v3.6.0.220655, Xamarin.Essentials v1.0.1, Visual Studio 2017 v15.9.12.
I am very comfortable with developing apps using Xamarin but very new to merging ResourceDictionaries in Xamarin (which I thought would be similar to WPF merging).

The apps I develop have a very consistent style, and I define the ResourceDictionary for each app separately in the App.xaml in the PCL/Portable/Main Project (I am not sure what to call this since moving over to .NetStandard 2.0).

The Problem: Homemade Bug is present in 20+ apps

We recently found a minor bug that we introduced in a work-around we made years ago, and I face the task of updating this in about 20+ apps.
To avoid this happening again I want to create a "Common" project that can be referenced by multiple Xamarin solutions, and move the problem-page into this "Common" project.

What I want to do: "Common" project for all 20+ apps - then merge ResourceDictionaries

One thing I would like to do is create a ResourceDictionary that is inside this Common project then use it in any app that merges the ResourceDictionary of the Common project.

Is this possible?

I have noticed that the pages I create in this Common can get Styles from the App.xaml, but I want to go in the opposite direction: the app gets the styles from the Common project.

What I have now

I have squashed the Exception where it does not find the ResourceDictionary I wanted to merge.
Now I have get exception that my {StaticResource DefCaptionHeight} cannot be found.

Here is how my Common ResourceDictionary appears in 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"
             xmlns:xfc="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.Core"
             x:Class="XamarinCommon.XamarinStyles">

    <OnPlatform x:Key="DefCaptionTitle" x:TypeArguments="xfc:GridLength" iOS="22" Android="26" WinPhone="22" />
</ResourceDictionary>
    ~~~~

Here is the code-behind for the Common ResourceDictionary:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace XamarinCommon
{
public partial class XamarinStyles : ResourceDictionary
{
public XamarinStyles()
{

        }
}

}

Here is where I merge in the App (or should I be merging in every page?):

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

             xmlns:xc="clr-namespace:XamarinCommon;assembly=XamarinCommon"

             xmlns:xfc="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.Core">
    <Application.Resources>

        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries> 

                <xc:XamarinStyles/>

            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>

    </Application.Resources>
</Application>

Here is the page in the Portable Class where I try to get DefCaptionTitle:

<?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:local="clr-namespace:TestApp"     
             x:Class="TestApp.TestPage" 
             BackgroundColor="#F2F2F2">
    <ContentPage.Content>

        <Grid HorizontalOptions="FillAndExpand"  VerticalOptions="FillAndExpand">

            <Grid.RowDefinitions>
                <RowDefinition Height="{StaticResource DefCaptionTitle}"></RowDefinition>
                <RowDefinition Height="1*"></RowDefinition>                
            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"></ColumnDefinition>                
            </Grid.ColumnDefinitions>
        </Grid>

    </ContentPage.Content>
</ContentPage>

Or?

Or is it better to abandon this idea and continue defining the ResourceDictionary in the app, then locally defining any specific resources I need for pages that exist in the Common project?

Thanks.
I am unsure why I cannot make the C# code appear normally in a Code-Block:

Sign In or Register to comment.