Using ResourceDictionary.MergedDictionaries in App.xaml

In VS2017 with Xamarin

In mine app.xaml, I have a MergedDictionary that references a xaml containing my DataTemplate. It is not recognized on the content page. If I move the DataTemplate to app.xaml, it works fine.

How do I get <ResourceDictionary.MergedDictionaries>to recognize the StaticResource defined in 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>
....
+4
source share
2 answers

It is possible to create a custom ResourceDictionary with support for multiple joins.

: 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; }
}

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

        <AnotherResource x:Key="Key1" />
    </m:ResourceDictionary>
</Application.Resources>
+3

, Xamarin.Forms, . MergedDictionary

:

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>

, , + .

, ResourceDictionary Xamarin , . , github.

, .

+2

Source: https://habr.com/ru/post/1679678/


All Articles