Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ResourceDictionary.MergedDictionaries in App.xaml

In VS2017 with Xamarin

In my app.xaml I have a MergedDictionary which references a xaml containing my DataTemplate. It isn't being recognized by a content page. If I move the DataTemplate into the app.xaml it works fine.

How do I get the <ResourceDictionary.MergedDictionaries> to recognize the StaticResource defined in the CellTemplates.xaml ?

My App.xaml:

<Application.Resources>
   <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources/CellTemplates.xaml"/>
        </ResourceDictionary.MergedDictionaries>
   ....

My CellTemplates.xaml:

 <ResourceDictionary
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <DataTemplate x:Key="CustomerTemplate">
    <ViewCell Height="100">
          <StackLayout VerticalOptions="FillAndExpand">
     ....

My Content Page:

<?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:Muffin"
         x:Class="Muffin.MainPage">
<ScrollView>
    <ListView ItemsSource="{Binding Customers}" 
              HasUnevenRows="True" 
              ItemTemplate="{StaticResource CustomerTemplate}">
    </ListView>
....
like image 312
Ian Vink Avatar asked Jun 19 '17 21:06

Ian Vink


People also ask

How do I add a resource dictionary to XAML?

Tip You can create a resource dictionary file in Microsoft Visual Studio by using the Add > New Item… > Resource Dictionary option from the Project menu. Here, you define a resource dictionary in a separate XAML file called Dictionary1. xaml.

How do I merge a resource dictionary in WPF?

To add a merged dictionary through code, you obtain a reference to the desired primary ResourceDictionary , get its MergedDictionaries property value, and call Add on the generic Collection that's contained in MergedDictionaries . The object you add must be a new ResourceDictionary .

What is ResourceDictionary?

A resource dictionary is a repository for XAML resources, such as styles, that your app uses. You define the resources in XAML and can then retrieve them in XAML using the {StaticResource} markup extension and {ThemeResource} markup extension s. You can also access resources with code, but that is less common.

How do I add a resource to a project in WPF?

To add a Resource Dictionary into your WPF application, right click the WPF project > add a Resource Dictionary. Now apply the resource "myAnotherBackgroundColor" to button background and observe the changes.


2 Answers

It's possible to create custom ResourceDictionary with multiple merging support.

See: https://github.com/Makeloft/Ace/blob/master/Ace.Zest/Markup/ResourceDictionary.cs

internal static class MergeExtensions
{
    internal static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
    {
        foreach (var item in items) action(item);
    }

    internal static void Merge<TKey, TValue>(this IDictionary<TKey, TValue> targetDictionary,
        IEnumerable<KeyValuePair<TKey, TValue>> sourceItems)
    {
        var targetItems = targetDictionary.ToArray();
        sourceItems.ForEach(i => targetDictionary[i.Key] = i.Value);
        targetItems.ForEach(i => targetDictionary[i.Key] = i.Value); // override merged by local values
    }
}

public class ResourceDictionary : Xamarin.Forms.ResourceDictionary
{
    public ResourceDictionary()
    {
        MergedDictionaries = new ObservableCollection<Xamarin.Forms.ResourceDictionary>();
        MergedDictionaries.CollectionChanged += (sender, args) =>
            (args.NewItems ?? new List<object>()).OfType<Xamarin.Forms.ResourceDictionary>()
            .ForEach(this.Merge);
    }

    public ObservableCollection<Xamarin.Forms.ResourceDictionary> MergedDictionaries { get; }
}

Usage

<Application.Resources>
    <m:ResourceDictionary>
        <m:ResourceDictionary.MergedDictionaries>
            <local:AppConverters />
        </m:ResourceDictionary.MergedDictionaries>

        <AnotherResource x:Key="Key1" />
    </m:ResourceDictionary>
</Application.Resources>
like image 157
Makeman Avatar answered Oct 23 '22 03:10

Makeman


You are trying to use a functionality that is not currently available in Xamarin.Forms and that's the use of multiple Merged Dictionaries. At the moment you can only have a single MergedDictionary

Try this in your code:

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:local="clr-namespace:Muffin"
    x:Class="Muffin.App">
    <Application.Resources>
        <ResourceDictionary MergedWith="local:CellTemplates">
                  ...
            <!--YOUR APP LEVEL STYLES -->
                  ...
        </ResourceDictionary>
    </Application.Resources>
</Application>

Note that to merge the Resource is not used the physical file location instead it's used the full namespace + class name.

Good news is that the possibility of merging more than one ResourceDictionary in Xamarin is under development and it should be available any soon. If you want to follow the advances of this feature subscribe to this thread in github.

Hope this helps.-

like image 40
pinedax Avatar answered Oct 23 '22 03:10

pinedax