在App.xaml中使用ResourceDictionary.MergedDictionaries

时间:2017-06-19 21:52:17

标签: c# xaml xamarin xamarin.forms

在VS2017中使用Xamarin

在我的app.xaml中,我有一个MergedDictionary,它引用了一个包含我的DataTemplate的xaml。它不被内容页面识别。如果我将DataTemplate移动到app.xaml,它可以正常工作。

如何让<ResourceDictionary.MergedDictionaries>识别CellTemplates.xaml中定义的StaticResource?

我的App.xaml:

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

我的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">
     ....

我的内容页面:

<?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>
....

2 个答案:

答案 0 :(得分:3)

可以创建具有多个合并支持的自定义ResourceDictionary。

请参阅: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>

答案 1 :(得分:2)

您正在尝试使用Xamarin.Forms中当前不可用的功能,这是使用多个Merged Dictionaries。目前您只能拥有一个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>

请注意,合并资源不会使用物理文件位置,而是使用完整的命名空间+类名。

好消息是,在Xamarin中合并多个ResourceDictionary的可能性正在开发中,它应该很快就可以使用。如果您想按照此功能的进展,请订阅github中的this线程。

希望这会有所帮助.-

相关问题