Skip to content

Commit b1c705f

Browse files
fix BPConverter
1 parent 47ff8b3 commit b1c705f

File tree

4 files changed

+64
-15
lines changed

4 files changed

+64
-15
lines changed

src/Controls/src/Build.Tasks/CompiledConverters/BindablePropertyConverter.cs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ public IEnumerable<Instruction> ConvertFromString(string value, ILContext contex
2525
yield return Instruction.Create(OpCodes.Ldsfld, bpRef);
2626
}
2727

28+
static bool IsOfAnyType(XmlType xmlType, params string[] types)
29+
{
30+
if (types == null || types.Length == 0)
31+
return false;
32+
if (xmlType == null)
33+
return false;
34+
if (xmlType.NamespaceUri != XamlParser.MauiUri && xmlType.NamespaceUri != XamlParser.MauiGlobalUri)
35+
return false;
36+
if (types.Contains(xmlType.Name))
37+
return true;
38+
return false;
39+
}
2840
public FieldReference GetBindablePropertyFieldReference(string value, ILContext context, ModuleDefinition module, BaseNode node)
2941
{
3042
FieldReference bpRef = null;
@@ -34,24 +46,18 @@ public FieldReference GetBindablePropertyFieldReference(string value, ILContext
3446
if (parts.Length == 1)
3547
{
3648
var parent = node.Parent?.Parent as IElementNode ?? (node.Parent?.Parent as IListNode)?.Parent as IElementNode;
37-
if ((node.Parent as ElementNode)?.XmlType.NamespaceUri == XamlParser.MauiUri
38-
&& ((node.Parent as ElementNode)?.XmlType.Name == nameof(Setter)
39-
|| (node.Parent as ElementNode)?.XmlType.Name == nameof(PropertyCondition)))
49+
if (IsOfAnyType((node.Parent as ElementNode)?.XmlType, nameof(Setter), nameof(PropertyCondition)))
4050
{
41-
if (parent.XmlType.NamespaceUri == XamlParser.MauiUri &&
42-
(parent.XmlType.Name == nameof(Trigger)
43-
|| parent.XmlType.Name == nameof(DataTrigger)
44-
|| parent.XmlType.Name == nameof(MultiTrigger)
45-
|| parent.XmlType.Name == nameof(Style)))
51+
if (IsOfAnyType(parent.XmlType, nameof(Trigger), nameof(DataTrigger), nameof(MultiTrigger), nameof(Style)))
4652
{
4753
typeName = GetTargetTypeName(parent);
4854
}
49-
else if (parent.XmlType.NamespaceUri == XamlParser.MauiUri && parent.XmlType.Name == nameof(VisualState))
55+
else if (IsOfAnyType(parent.XmlType, nameof(VisualState)))
5056
{
5157
typeName = FindTypeNameForVisualState(parent, node);
5258
}
5359
}
54-
else if ((node.Parent as ElementNode)?.XmlType.NamespaceUri == XamlParser.MauiUri && (node.Parent as ElementNode)?.XmlType.Name == nameof(Trigger))
60+
else if (IsOfAnyType((node.Parent as ElementNode)?.XmlType, nameof(Trigger)))
5561
{
5662
typeName = GetTargetTypeName(node.Parent);
5763
}
@@ -86,19 +92,19 @@ static string FindTypeNameForVisualState(IElementNode parent, IXmlLineInfo lineI
8692
//1. parent is VisualState, don't check that
8793

8894
//2. check that the VS is in a VSG
89-
if (!(parent.Parent is IElementNode target) || target.XmlType.NamespaceUri != XamlParser.MauiUri || target.XmlType.Name != nameof(VisualStateGroup))
95+
// if (!(parent.Parent is IElementNode target) || target.XmlType.NamespaceUri != XamlParser.MauiUri || target.XmlType.Name != nameof(VisualStateGroup))
96+
if (!(parent.Parent is IElementNode target) || !IsOfAnyType(target.XmlType, nameof(VisualStateGroup)))
9097
throw new XamlParseException($"Expected {nameof(VisualStateGroup)} but found {parent.Parent}", lineInfo);
9198

9299
//3. if the VSG is in a VSGL, skip that as it could be implicit
93100
if (target.Parent is ListNode
94-
|| ((target.Parent as IElementNode)?.XmlType.NamespaceUri == XamlParser.MauiUri
95-
&& (target.Parent as IElementNode)?.XmlType.Name == nameof(VisualStateGroupList)))
101+
|| IsOfAnyType((target.Parent as IElementNode)?.XmlType, nameof(VisualStateGroupList)))
96102
target = target.Parent.Parent as IElementNode;
97103
else
98104
target = target.Parent as IElementNode;
99105

100106
//4. target is now a Setter in a Style, or a VE
101-
if (target.XmlType.NamespaceUri == XamlParser.MauiUri && target.XmlType.Name == nameof(Setter))
107+
if (IsOfAnyType(target.XmlType, nameof(Setter)))
102108
return ((target?.Parent as IElementNode)?.Properties[new XmlName("", "TargetType")] as ValueNode)?.Value as string;
103109
else
104110
return target.XmlType.Name;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ static bool TryCompileBindingPath(ElementNode node, ILContext context, VariableD
533533
}
534534

535535
if (n.XmlType.Name == nameof(Microsoft.Maui.Controls.DataTemplate)
536-
&& n.XmlType.NamespaceUri == XamlParser.MauiUri)
536+
&& (n.XmlType.NamespaceUri == XamlParser.MauiUri) || n.XmlType.NamespaceUri == XamlParser.MauiGlobalUri)
537537
{
538538
xDataTypeIsInOuterScope = true;
539539
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.GlobalXmlnsWithStyle"
3+
Title="GlobalXmlnsWithStyle">
4+
<ContentPage.Resources>
5+
<Style TargetType="Label">
6+
<Setter Property="TextColor" Value="Red" />
7+
<Setter Property="FontSize" Value="24" />
8+
</Style>
9+
</ContentPage.Resources>
10+
<Label x:Name="label0"
11+
Text="Welcome to .NET MAUI!"
12+
VerticalOptions="Center"
13+
HorizontalOptions="Center" />
14+
</ContentPage>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Microsoft.Maui.Graphics;
2+
using NUnit.Framework;
3+
4+
namespace Microsoft.Maui.Controls.Xaml.UnitTests;
5+
6+
// [XamlCompilation(XamlCompilationOptions.Skip)]
7+
public partial class GlobalXmlnsWithStyle : ContentPage
8+
{
9+
public GlobalXmlnsWithStyle()
10+
{
11+
InitializeComponent();
12+
}
13+
public GlobalXmlnsWithStyle(bool useCompiledXaml)
14+
{
15+
// this stub will be replaced at compile time
16+
}
17+
18+
[Test]
19+
public void GlobalXmlnsWithStyleTest([Values] bool useCompiledXaml)
20+
{
21+
// if (useCompiledXaml)
22+
// MockCompiler.Compile(typeof(GlobalXmlnsWithStyle));
23+
// else
24+
// {
25+
var page = new GlobalXmlnsWithStyle(useCompiledXaml);
26+
Assert.That(page.label0.TextColor, Is.EqualTo(Colors.Red));
27+
// }
28+
}
29+
}

0 commit comments

Comments
 (0)