Browse Source

Fixed item type inference in compiled bindings.

pull/4291/head
José Pedro 6 years ago
parent
commit
05154391fb
No known key found for this signature in database GPG Key ID: B8247B9301707B83
  1. 29
      src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlDataContextTypeTransformer.cs

29
src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlDataContextTypeTransformer.cs

@ -1,11 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Avalonia.Markup.Parsers;
using Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers;
using Avalonia.Utilities;
using XamlX;
using XamlX.Ast;
using XamlX.Transform;
@ -129,12 +124,13 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers
if (itemsCollectionType != null)
{
var elementType = itemsCollectionType
.GetAllInterfaces()
.FirstOrDefault(i =>
i.GenericTypeDefinition?.Equals(context.Configuration.WellKnownTypes.IEnumerableT) == true)
.GenericArguments[0];
return new AvaloniaXamlIlDataContextTypeMetadataNode(on, elementType);
foreach (var i in GetAllInterfacesIncludingSelf(itemsCollectionType))
{
if (i.GenericTypeDefinition?.Equals(context.Configuration.WellKnownTypes.IEnumerableT) == true)
{
return new AvaloniaXamlIlDataContextTypeMetadataNode(on, i.GenericArguments[0]);
}
}
}
// We can't infer the collection type and the currently calculated type is definitely wrong.
// Notify the user that we were unable to infer the data context type if they use a compiled binding.
@ -165,6 +161,15 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers
return new AvaloniaXamlIlUninferrableDataContextMetadataNode(on);
}
private static IEnumerable<IXamlType> GetAllInterfacesIncludingSelf(IXamlType type)
{
if (type.IsInterface)
yield return type;
foreach (var i in type.GetAllInterfaces())
yield return i;
}
}
[DebuggerDisplay("DataType = {DataContextType}")]

Loading…
Cancel
Save