![](/img/trans.png)
[英]Different ways of using ResourceDictionary.MergedDictionaries
[英]Using ResourceDictionary.MergedDictionaries in App.xaml
在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>
....
可以创建具有多个合并支持的自定义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>
您正在尝试使用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中订阅这个主题。
希望这可以帮助。-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.