Skip to content

Commit 46ad78f

Browse files
[XC] Add support for DoesNotInheritDataTypeAttribute to XamlC (#26359)
* Add test * Implement DoesNotInheritDataTypeAttribute support in XamlC * Update related test
1 parent d999d56 commit 46ad78f

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs

+14
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,10 @@ static bool TryCompileBindingPath(ElementNode node, ILContext context, VariableD
459459
{
460460
break;
461461
}
462+
if (DoesNotInheritDataType(n))
463+
{
464+
break;
465+
}
462466

463467
if (n.XmlType.Name == nameof(Microsoft.Maui.Controls.DataTemplate)
464468
&& n.XmlType.NamespaceUri == XamlParser.MauiUri)
@@ -585,6 +589,16 @@ static bool IsBindingContextBinding(ElementNode node)
585589
&& propertyName.NamespaceURI == ""
586590
&& propertyName.LocalName == nameof(BindableObject.BindingContext);
587591
}
592+
593+
bool DoesNotInheritDataType(IElementNode node)
594+
{
595+
return GetParent(node) is IElementNode parentNode
596+
&& node.TryGetPropertyName(parentNode, out XmlName propertyName)
597+
&& parentNode.XmlType.TryGetTypeReference(context.Cache, module, (IXmlLineInfo)node, out TypeReference parentTypeRef)
598+
&& parentTypeRef.ResolveCached(context.Cache) is TypeDefinition parentType
599+
&& parentType.GetProperty(context.Cache, pd => pd.Name == propertyName.LocalName, out var propertyDeclaringTypeRef) is PropertyDefinition propertyDef
600+
&& propertyDef.CustomAttributes.Any(ca => ca.AttributeType.FullName == "Microsoft.Maui.Controls.Xaml.DoesNotInheritDataTypeAttribute");
601+
}
588602
}
589603

590604
static bool TryParsePath(ILContext context, string path, TypeReference tSourceRef, IXmlLineInfo lineInfo, ModuleDefinition module, out IList<(PropertyDefinition property, TypeReference propDeclTypeRef, string indexArg)> pathProperties)

src/Controls/tests/Xaml.UnitTests/Issues/Maui23989.xaml.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void Setup()
3737
public void ItemDisplayBindingWithoutDataTypeFails([Values(false, true)] bool useCompiledXaml)
3838
{
3939
if (useCompiledXaml)
40-
Assert.Throws(new BuildExceptionConstraint(12, 13, s => s.Contains("0045", StringComparison.Ordinal)), () => MockCompiler.Compile(typeof(Maui23989), null, true));
40+
Assert.Throws(new BuildExceptionConstraint(12, 13, s => s.Contains("0022", StringComparison.Ordinal)), () => MockCompiler.Compile(typeof(Maui23989), treatWarningsAsErrors: true));
4141

4242
var layout = new Maui23989(useCompiledXaml);
4343
//without x:DataType, bindings aren't compiled
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests"
5+
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui25935"
6+
x:DataType="local:Maui25935">
7+
<Picker x:Name="Picker"
8+
ItemDisplayBinding="{Binding .}">
9+
<Picker.ItemsSource>
10+
<x:Array Type="{x:Type x:Int32}">
11+
<x:Int32>1</x:Int32>
12+
<x:Int32>2</x:Int32>
13+
<x:Int32>3</x:Int32>
14+
</x:Array>
15+
</Picker.ItemsSource>
16+
</Picker>
17+
</ContentPage>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using Microsoft.Maui.ApplicationModel;
5+
using Microsoft.Maui.Controls.Core.UnitTests;
6+
using Microsoft.Maui.Dispatching;
7+
using Microsoft.Maui.Graphics;
8+
using Microsoft.Maui.UnitTests;
9+
using NUnit.Framework;
10+
11+
namespace Microsoft.Maui.Controls.Xaml.UnitTests;
12+
13+
public partial class Maui25935
14+
{
15+
public Maui25935()
16+
{
17+
InitializeComponent();
18+
}
19+
20+
public Maui25935(bool useCompiledXaml)
21+
{
22+
//this stub will be replaced at compile time
23+
}
24+
25+
[TestFixture]
26+
class Test
27+
{
28+
[SetUp]
29+
public void Setup()
30+
{
31+
Application.SetCurrentApplication(new MockApplication());
32+
DispatcherProvider.SetCurrent(new DispatcherProviderStub());
33+
}
34+
35+
[TearDown] public void TearDown() => AppInfo.SetCurrent(null);
36+
37+
[Test]
38+
public void ToolBarItemAppThemeBinding([Values(false, true)] bool useCompiledXaml)
39+
{
40+
var page = new Maui25935(useCompiledXaml);
41+
var items = page.Picker.Items.ToArray();
42+
Assert.Contains("1", items);
43+
Assert.Contains("2", items);
44+
Assert.Contains("3", items);
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)