【问题标题】:Using ResourceDictionary.MergedDictionaries in App.xaml在 App.xaml 中使用 ResourceDictionary.MergedDictionaries
【发布时间】:2017-06-19 21:52:17
【问题描述】:

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

【问题讨论】:

    标签: c# xaml xamarin xamarin.forms


    【解决方案1】:

    可以创建具有多个合并支持的自定义 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 的可能性正在开发中,应该很快就会可用。如果您想关注此功能的进展,请订阅 github 中的this 线程。

      希望这会有所帮助。-

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多