Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,10 @@ static bool TryCompileBindingPath(ElementNode node, ILContext context, VariableD
{
break;
}
if (DoesNotInheritDataType(n))
{
break;
}

if (n.XmlType.Name == nameof(Microsoft.Maui.Controls.DataTemplate)
&& n.XmlType.NamespaceUri == XamlParser.MauiUri)
Expand Down Expand Up @@ -585,6 +589,16 @@ static bool IsBindingContextBinding(ElementNode node)
&& propertyName.NamespaceURI == ""
&& propertyName.LocalName == nameof(BindableObject.BindingContext);
}

bool DoesNotInheritDataType(IElementNode node)
{
return GetParent(node) is IElementNode parentNode
&& node.TryGetPropertyName(parentNode, out XmlName propertyName)
&& parentNode.XmlType.TryGetTypeReference(context.Cache, module, (IXmlLineInfo)node, out TypeReference parentTypeRef)
&& parentTypeRef.ResolveCached(context.Cache) is TypeDefinition parentType
&& parentType.GetProperty(context.Cache, pd => pd.Name == propertyName.LocalName, out var propertyDeclaringTypeRef) is PropertyDefinition propertyDef
&& propertyDef.CustomAttributes.Any(ca => ca.AttributeType.FullName == "Microsoft.Maui.Controls.Xaml.DoesNotInheritDataTypeAttribute");
}
}

static bool TryParsePath(ILContext context, string path, TypeReference tSourceRef, IXmlLineInfo lineInfo, ModuleDefinition module, out IList<(PropertyDefinition property, TypeReference propDeclTypeRef, string indexArg)> pathProperties)
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/tests/Xaml.UnitTests/Issues/Maui23989.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void Setup()
public void ItemDisplayBindingWithoutDataTypeFails([Values(false, true)] bool useCompiledXaml)
{
if (useCompiledXaml)
Assert.Throws(new BuildExceptionConstraint(12, 13, s => s.Contains("0045", StringComparison.Ordinal)), () => MockCompiler.Compile(typeof(Maui23989), null, true));
Assert.Throws(new BuildExceptionConstraint(12, 13, s => s.Contains("0022", StringComparison.Ordinal)), () => MockCompiler.Compile(typeof(Maui23989), treatWarningsAsErrors: true));

var layout = new Maui23989(useCompiledXaml);
//without x:DataType, bindings aren't compiled
Expand Down
17 changes: 17 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui25935.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests"
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui25935"
x:DataType="local:Maui25935">
<Picker x:Name="Picker"
ItemDisplayBinding="{Binding .}">
<Picker.ItemsSource>
<x:Array Type="{x:Type x:Int32}">
<x:Int32>1</x:Int32>
<x:Int32>2</x:Int32>
<x:Int32>3</x:Int32>
</x:Array>
</Picker.ItemsSource>
</Picker>
</ContentPage>
47 changes: 47 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui25935.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Controls.Core.UnitTests;
using Microsoft.Maui.Dispatching;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.UnitTests;
using NUnit.Framework;

namespace Microsoft.Maui.Controls.Xaml.UnitTests;

public partial class Maui25935
{
public Maui25935()
{
InitializeComponent();
}

public Maui25935(bool useCompiledXaml)
{
//this stub will be replaced at compile time
}

[TestFixture]
class Test
{
[SetUp]
public void Setup()
{
Application.SetCurrentApplication(new MockApplication());
DispatcherProvider.SetCurrent(new DispatcherProviderStub());
}

[TearDown] public void TearDown() => AppInfo.SetCurrent(null);

[Test]
public void ToolBarItemAppThemeBinding([Values(false, true)] bool useCompiledXaml)
{
var page = new Maui25935(useCompiledXaml);
var items = page.Picker.Items.ToArray();
Assert.Contains("1", items);
Assert.Contains("2", items);
Assert.Contains("3", items);
}
}
}