Skip to content

Commit

Permalink
fix: Handle nullable value types correctly in BindableTypeProvidersSo…
Browse files Browse the repository at this point in the history
…urceGenerator
  • Loading branch information
Youssef1313 committed Mar 25, 2024
1 parent e795d80 commit b8b580b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -376,12 +376,11 @@ where field.IsStatic
&& property.SetMethod.IsLocallyPublic(_currentModule!)
)
{
if (property.Type.IsValueType)
{
writer.AppendLineIndented($@"bindableType.AddProperty(""{propertyName}"", typeof({propertyTypeName}), Get{propertyName}, Set{propertyName});");

postWriter.AppendLineIndented($@"private static object Get{propertyName}(object instance, Microsoft.UI.Xaml.DependencyPropertyValuePrecedences? precedence) => (({ownerTypeName})instance).{propertyName};");
writer.AppendLineIndented($@"bindableType.AddProperty(""{propertyName}"", typeof({propertyTypeName}), Get{propertyName}, Set{propertyName});");
postWriter.AppendLineIndented($@"private static object Get{propertyName}(object instance, Microsoft.UI.Xaml.DependencyPropertyValuePrecedences? precedence) => (({ownerTypeName})instance).{propertyName};");

if (property.Type.IsValueType && property.Type.OriginalDefinition.SpecialType != SpecialType.System_Nullable_T)
{
using (postWriter.BlockInvariant($@"private static void Set{propertyName}(object instance, object value, Microsoft.UI.Xaml.DependencyPropertyValuePrecedences? precedence)"))
{
using (postWriter.BlockInvariant($"if(value != null)"))
Expand All @@ -392,9 +391,6 @@ where field.IsStatic
}
else
{
writer.AppendLineIndented($@"bindableType.AddProperty(""{propertyName}"", typeof({propertyTypeName}), Get{propertyName}, Set{propertyName});");

postWriter.AppendLineIndented($@"private static object Get{propertyName}(object instance, Microsoft.UI.Xaml.DependencyPropertyValuePrecedences? precedence) => (({ownerTypeName})instance).{propertyName};");
postWriter.AppendLineIndented($@"private static void Set{propertyName}(object instance, object value, Microsoft.UI.Xaml.DependencyPropertyValuePrecedences? precedence) => (({ownerTypeName})instance).{propertyName} = ({propertyTypeName})value;");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Page
x:Class="Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Data.BindableNullableValueTypeTestPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Data"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid>
<TextBlock Tag="{Binding MyProperty, Mode=TwoWay}" x:FieldModifier="public" x:Name="textBlock" />
</Grid>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#nullable enable

using System.ComponentModel;
using Microsoft.UI.Xaml.Controls;

namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Data;

[Bindable(true)]
public sealed partial class BindableNullableValueTypeTestPage : Page, INotifyPropertyChanged
{
private int? _myProperty;

public BindableNullableValueTypeTestPage()
{
this.InitializeComponent();
this.DataContext = this;
}

public event PropertyChangedEventHandler? PropertyChanged;

public int? MyProperty
{
get => _myProperty;
set
{
if (_myProperty != value)
{
_myProperty = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(MyProperty)));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;

namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Data;

[TestClass]
public class Given_BindableNullableValueType
{
[TestMethod]
[RunsOnUIThread]
public void When_BindableNullableValueTypeTestPage()
{
var x = new BindableNullableValueTypeTestPage();
var tb = x.textBlock;
tb.Tag = "10";
Assert.AreEqual(10, x.MyProperty);
tb.Tag = null;
Assert.AreEqual(null, x.MyProperty);
}
}

0 comments on commit b8b580b

Please sign in to comment.