2017-06-19 4 views
4

In VS2017 mit XamarinMit ResourceDictionary.MergedDictionaries in App.xaml

In meinem app.xaml habe ich eine MergedDictionary, die eine XAML enthält meine Datatemplate verweist. Es wird von einer Inhaltsseite nicht erkannt. Wenn ich das DataTemplate in die app.xaml verschiebe, funktioniert es gut.

Wie bekomme ich die <ResourceDictionary.MergedDictionaries>, um die StaticResource zu erkennen, die in CellTemplates.xaml definiert ist?

Mein App.xaml:

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

Mein 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"> 
    .... 

Mein Inhalt Seite:

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

Antwort

4

Es ist möglich, eigene erstellen Resource mit mehreren Unterstützung verschmelzen.

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

Nutzungs

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

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

Sie versuchen, eine Funktion zu verwenden, die nicht aktuell ist verfügbar in Xamarin.Forms und das ist die Verwendung von mehreren zusammengeführten Wörterbüchern. Im Moment können Sie nur eine einzige MergedDictionary

die Sie interessieren im 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> 

Hinweis, dass die Ressource zu verschmelzen ist nicht die physikalische Speicherort der Datei stattdessen verwendet Es wird der vollständige Namespace + Klassenname verwendet.

Gute Nachricht ist, dass die Möglichkeit, mehr als ein ResourceDictionary in Xamarin zu verschmelzen, gerade entwickelt wird und bald verfügbar sein sollte. Wenn Sie die Fortschritte dieser Funktion verfolgen möchten, abonnieren Sie this Thread in GitHub.

Hope this helps.-

Verwandte Themen