2017-06-19 48 views
4

使用ResourceDictionary.MergedDictionaries在VS2017與Xamarin在App.xaml中

在我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> 
.... 

回答

4

可以創建具有多個合併支持的自定義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> 
2

您嘗試使用功能,目前不在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> 

注意,合併資源不使用物理文件位置,而不是它使用完整的名稱空間+類名稱。

好消息是,在Xamarin中合併多個ResourceDictionary的可能性正在開發中,它應該很快就可用。如果你想跟隨這個功能的進步訂閱this線程在github。

希望這會有所幫助.-

相關問題