Skip to content

[XC] better support for nullable props and BPs #28550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 23, 2025
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
61 changes: 43 additions & 18 deletions src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,25 @@ static IEnumerable<Instruction> SetBinding(VariableDefinition parent, FieldRefer

static bool CanSetValue(FieldReference bpRef, bool attached, INode node, IXmlLineInfo iXmlLineInfo, ILContext context)
{
static bool CanSetValue (TypeReference bpTypeRef, VariableDefinition varValue, ILContext context, IXmlLineInfo iXmlLineInfo)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extracting the logic in a local method to run it twice, once on the nullable type, once on the inner type

{
// If it's an attached BP, there's no second chance to handle IMarkupExtensions, so we try here.
// Worst case scenario ? InvalidCastException at runtime
if (varValue.VariableType.FullName == "System.Object")
return true;
var implicitOperator = varValue.VariableType.GetImplicitOperatorTo(context.Cache, bpTypeRef, context.Body.Method.Module);
if (implicitOperator != null)
return true;

//as we're in the SetValue Scenario, we can accept value types, they'll be boxed
if (varValue.VariableType.IsValueType && bpTypeRef.FullName == "System.Object")
return true;

if (varValue.VariableType.InheritsFromOrImplements(context.Cache, bpTypeRef) || varValue.VariableType.FullName == "System.Object")
return true;
return false;
}

var module = context.Body.Method.Module;

if (bpRef == null)
Expand All @@ -1424,22 +1443,23 @@ static bool CanSetValue(FieldReference bpRef, bool attached, INode node, IXmlLin
if (!context.Variables.TryGetValue(elementNode, out VariableDefinition varValue))
return false;


var bpTypeRef = bpRef.GetBindablePropertyType(context.Cache, iXmlLineInfo, module);
// If it's an attached BP, there's no second chance to handle IMarkupExtensions, so we try here.
// Worst case scenario ? InvalidCastException at runtime
if (varValue.VariableType.FullName == "System.Object")
return true;
var implicitOperator = varValue.VariableType.GetImplicitOperatorTo(context.Cache, bpTypeRef, module);
if (implicitOperator != null)
return true;

//as we're in the SetValue Scenario, we can accept value types, they'll be boxed
if (varValue.VariableType.IsValueType && bpTypeRef.FullName == "System.Object")
if (CanSetValue(bpTypeRef, varValue, context, iXmlLineInfo))
return true;

return varValue.VariableType.InheritsFromOrImplements(context.Cache, bpTypeRef) || varValue.VariableType.FullName == "System.Object";
if (bpTypeRef.ResolveCached(context.Cache).FullName == "System.Nullable`1")
{
bpTypeRef = ((GenericInstanceType)bpTypeRef).GenericArguments[0];
if (CanSetValue(bpTypeRef, varValue, context, iXmlLineInfo))
return true;
}

return false;
}


static bool CanGetValue(VariableDefinition parent, FieldReference bpRef, bool attached, IXmlLineInfo iXmlLineInfo, ILContext context, out TypeReference propertyType)
{
var module = context.Body.Method.Module;
Expand Down Expand Up @@ -1483,32 +1503,37 @@ static IEnumerable<Instruction> SetValue(VariableDefinition parent, FieldReferen
var @else = Create(OpCodes.Nop);
var endif = Create(OpCodes.Nop);

if (context.Variables[elementNode].VariableType.FullName == "System.Object")

var varValue = context.Variables[elementNode];
if (varValue.VariableType.FullName == "System.Object")
{
//if(value != null && value.GetType().IsAssignableFrom(typeof(BindingBase)))
yield return Create(Ldloc, context.Variables[elementNode]);
yield return Create(Ldloc, varValue);
yield return Create(Brfalse, @else);

yield return Create(Ldtoken, module.ImportReference(context.Cache, ("Microsoft.Maui.Controls", "Microsoft.Maui.Controls", "BindingBase")));
yield return Create(Call, module.ImportMethodReference(context.Cache, ("mscorlib", "System", "Type"), methodName: "GetTypeFromHandle", parameterTypes: new[] { ("mscorlib", "System", "RuntimeTypeHandle") }, isStatic: true));
yield return Create(Ldloc, context.Variables[elementNode]);
yield return Create(Ldloc, varValue);
yield return Create(Callvirt, module.ImportMethodReference(context.Cache, ("mscorlib", "System", "Object"), methodName: "GetType", paramCount: 0));
yield return Create(Callvirt, module.ImportMethodReference(context.Cache, ("mscorlib", "System", "Type"), methodName: "IsAssignableFrom", parameterTypes: new[] { ("mscorlib", "System", "Type") }));
yield return Create(Brfalse, @else);
//then
yield return Create(Ldloc, context.Variables[elementNode]);
yield return Create(Ldloc, varValue);
yield return Create(Br, endif);
//else
yield return @else;
}
var bpTypeRef = bpRef.GetBindablePropertyType(context.Cache, iXmlLineInfo, module);
foreach (var instruction in context.Variables[elementNode].LoadAs(context.Cache, bpTypeRef, module))
foreach (var instruction in varValue.LoadAs(context.Cache, bpTypeRef, module))
yield return instruction;
if (bpTypeRef.IsValueType)
{
if ( bpTypeRef.ResolveCached(context.Cache).FullName == "System.Nullable`1"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the main change

&& TypeRefComparer.Default.Equals(varValue.VariableType, ((GenericInstanceType)bpTypeRef).GenericArguments[0]))
bpTypeRef = ((GenericInstanceType)bpTypeRef).GenericArguments[0];
yield return Create(Box, module.ImportReference(bpTypeRef));

}
//endif
if (context.Variables[elementNode].VariableType.FullName == "System.Object")
if (varValue.VariableType.FullName == "System.Object")
yield return endif;

}
Expand Down
7 changes: 5 additions & 2 deletions src/Controls/tests/Xaml.UnitTests/Issues/Bz24910.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public void ConversionForNullable(bool useCompiledXaml)
[TestCase(true), TestCase(false)]
public void AllowNull(bool useCompiledXaml)
{
if (useCompiledXaml)
MockCompiler.Compile(typeof(Bz24910));

var page = new Bz24910(useCompiledXaml);
var control = page.control2;
Assert.Null(control.NullableInt);
Expand Down Expand Up @@ -85,7 +88,7 @@ public void AllowNonBindableNullable(bool useCompiledXaml)
public class Bz24910Control : Button
{
public static readonly BindableProperty NullableIntProperty =
BindableProperty.Create("NullableInt", typeof(int?), typeof(Bz24910Control), default(int?));
BindableProperty.Create(nameof(NullableInt), typeof(int?), typeof(Bz24910Control), default(int?));

public int? NullableInt
{
Expand All @@ -94,7 +97,7 @@ public int? NullableInt
}

public static readonly BindableProperty NullableDoubleProperty =
BindableProperty.Create("NullableDouble", typeof(double?), typeof(Bz24910Control), default(double?));
BindableProperty.Create(nameof(NullableDouble), typeof(double?), typeof(Bz24910Control), default(double?));

public double? NullableDouble
{
Expand Down
38 changes: 21 additions & 17 deletions src/Controls/tests/Xaml.UnitTests/Issues/Unreported008.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests"
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Unreported008">
<DatePicker Date="{x:Static sys:DateTime.Now}" x:Name="picker0">
<DatePicker.Format>yyyy-MM-dd</DatePicker.Format>
<DatePicker.MinimumDate>
<sys:DateTime x:FactoryMethod="Parse">
<x:Arguments>
<x:String>Jan 1 2000</x:String>
</x:Arguments>
</sys:DateTime>
</DatePicker.MinimumDate>
<DatePicker.MaximumDate>
<sys:DateTime x:FactoryMethod="Parse">
<x:Arguments>
<x:String>Dec 31 2050</x:String>
</x:Arguments>
</sys:DateTime>
</DatePicker.MaximumDate>
</DatePicker>
<StackLayout>
<DatePicker Date="{x:Static sys:DateTime.Now}" x:Name="picker0">
<DatePicker.Format>yyyy-MM-dd</DatePicker.Format>
<DatePicker.MinimumDate>
<sys:DateTime x:FactoryMethod="Parse">
<x:Arguments>
<x:String>Jan 1 2000</x:String>
</x:Arguments>
</sys:DateTime>
</DatePicker.MinimumDate>
<DatePicker.MaximumDate>
<sys:DateTime x:FactoryMethod="Parse">
<x:Arguments>
<x:String>Dec 31 2050</x:String>
</x:Arguments>
</sys:DateTime>
</DatePicker.MaximumDate>
</DatePicker>
<local:Unreported008View Date="{x:Static sys:DateTime.Now}" x:Name="view0"/>
</StackLayout>
</ContentPage>
13 changes: 13 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Unreported008.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@

namespace Microsoft.Maui.Controls.Xaml.UnitTests
{
public class Unreported008View : ContentView
{
public static readonly BindableProperty DateProperty = BindableProperty.Create(nameof(Date), typeof(DateTime?), typeof(Unreported008View), null);

public DateTime? Date
{
get { return (DateTime?)GetValue(DateProperty); }
set { SetValue(DateProperty, value); }
}
}

public partial class Unreported008 : ContentPage
{
public Unreported008()
Expand All @@ -27,6 +38,8 @@ public void PickerDateTimesAndXamlC(bool useCompiledXaml)
Assert.AreEqual(DateTime.Today, picker.Date.Date);
Assert.AreEqual(new DateTime(2000, 1, 1), picker.MinimumDate);
Assert.AreEqual(new DateTime(2050, 12, 31), picker.MaximumDate);

Assert.AreEqual(DateTime.Today, page.view0.Date.Value.Date);
}
}
}
Expand Down
Loading