From 1de932ba8e139a1fead2009f2ba88c5c03b5d462 Mon Sep 17 00:00:00 2001 From: Andrey Kunchev Date: Tue, 7 Mar 2017 02:00:29 +0200 Subject: [PATCH] fixed Grid crash, update Grid.Column\Row definitions instead set new value --- .../PortableXaml/AvaloniaXamlType.cs | 47 +++++++++++++++++-- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaXamlType.cs b/src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaXamlType.cs index c24216741e..3cdcc52a62 100644 --- a/src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaXamlType.cs +++ b/src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaXamlType.cs @@ -90,16 +90,21 @@ namespace Avalonia.Markup.Xaml.PortableXaml new PropertyKey(typeof(Panel),nameof(Panel.Children)), }; + private static readonly List _updateListInsteadSet = + new List() + { + new PropertyKey(typeof(Grid),nameof(Grid.RowDefinitions)), + new PropertyKey(typeof(Grid),nameof(Grid.ColumnDefinitions)), + }; + protected override MethodInfo LookupUnderlyingSetter() { - var key = new PropertyKey(DeclaringType.UnderlyingType, Name); - //if we have content property a list //we have some issues in portable.xaml //but if the list is read only, this is solving the problem //TODO: investigate is this good enough as solution ??? //We can add ReadOnyAttribute to cover this - if (_readonlyProps.Contains(key)) + if (_readonlyProps.Contains(PropertyKey())) { return null; } @@ -109,7 +114,10 @@ namespace Avalonia.Markup.Xaml.PortableXaml protected override XamlMemberInvoker LookupInvoker() { - return new PropertyInvoker(this); + return new PropertyInvoker(this) + { + UpdateListInsteadSet = _updateListInsteadSet.Contains(PropertyKey()) + }; } protected override XamlType LookupType() @@ -150,8 +158,13 @@ namespace Avalonia.Markup.Xaml.PortableXaml return _dependsOn; } + private PropertyKey PropertyKey() + => new PropertyKey(DeclaringType.UnderlyingType, Name); + private class PropertyInvoker : XamlMemberInvoker { + public bool UpdateListInsteadSet { get; set; } = false; + public PropertyInvoker(XamlMember member) : base(member) { } @@ -169,6 +182,32 @@ namespace Avalonia.Markup.Xaml.PortableXaml value = (value as XamlBinding).Value; } + if (UpdateListInsteadSet && value != null) + { + object old = GetValue(instance); + + if (Equals(old, value)) + { + //don't set the same value + //usually a collections + return; + } + else if (old is IList && value is IList) + { + var oldList = (IList)old; + var curList = (IList)value; + + oldList.Clear(); + + foreach (object item in curList) + { + oldList.Add(item); + } + + return; + } + } + base.SetValue(instance, value); }