Skip to content

Commit

Permalink
fix(xbind): Possible invalid type cast in two-way mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed May 13, 2020
1 parent d3c70c3 commit b952235
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2884,7 +2884,7 @@ string buildBindBack()
if (propertyPaths.properties.Length == 1)
{
var targetPropertyType = GetXBindPropertyPathType(propertyPaths.properties[0], GetType(dataType));
return $"(___ctx, __value) => {{ if(___ctx is {dataType} ___tctx) {{ {contextFunction} = ({targetPropertyType})__value; }} }}";
return $"(___ctx, __value) => {{ if(___ctx is {dataType} ___tctx) {{ {contextFunction} = ({targetPropertyType})({propertyType})__value; }} }}";
}
else
{
Expand Down Expand Up @@ -2924,7 +2924,7 @@ string buildBindBack()
if (propertyPaths.properties.Length == 1)
{
var targetPropertyType = GetXBindPropertyPathType(propertyPaths.properties[0]);
return $"(___tctx, __value) => {rawFunction} = ({targetPropertyType})__value";
return $"(___tctx, __value) => {rawFunction} = ({targetPropertyType})({propertyType})__value";
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Page x:Class="Uno.UI.Tests.Windows_UI_Xaml_Data.xBindTests.Controls.Binding_TypeMismatch"
xmlns:sys="using:System"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Uno.UI.Tests.Windows_UI_Xaml_Data.xBindTests"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid>
<Slider x:Name="mySlider"
x:FieldModifier="public"
Value="{x:Bind MyInteger, Mode=TwoWay}"/>
</Grid>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238

namespace Uno.UI.Tests.Windows_UI_Xaml_Data.xBindTests.Controls
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class Binding_TypeMismatch : Page, System.ComponentModel.INotifyPropertyChanged
{
private int _myProperty;

public Binding_TypeMismatch()
{
this.InitializeComponent();
}

public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

public int MyInteger
{
get => _myProperty;
set
{
_myProperty = value;
PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(nameof(MyInteger)));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Page x:Class="Uno.UI.Tests.Windows_UI_Xaml_Data.xBindTests.Controls.Binding_TypeMismatch_DataTemplate"
xmlns:sys="using:System"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Uno.UI.Tests.Windows_UI_Xaml_Data.xBindTests.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid>
<ContentControl x:Name="root"
x:FieldModifier="public">
<ContentControl.ContentTemplate>
<DataTemplate x:DataType="local:Binding_TypeMismatch_DataTemplate_Data">
<Slider x:Name="mySlider"
x:FieldModifier="public"
Value="{x:Bind MyInteger, Mode=TwoWay}"/>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
</Grid>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238

namespace Uno.UI.Tests.Windows_UI_Xaml_Data.xBindTests.Controls
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class Binding_TypeMismatch_DataTemplate : Page
{

public Binding_TypeMismatch_DataTemplate()
{
this.InitializeComponent();
}
}

public class Binding_TypeMismatch_DataTemplate_Data : System.ComponentModel.INotifyPropertyChanged
{
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

private int _myProperty;

public int MyInteger
{
get => _myProperty;
set
{
_myProperty = value;
PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(nameof(MyInteger)));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text;
using System.Threading.Tasks;
using Uno.UI.Tests.Windows_UI_Xaml_Data.xBindTests.Controls;
using Windows.UI.Xaml.Controls;

namespace Uno.UI.Tests.Windows_UI_Xaml_Data.xBindTests
{
Expand Down Expand Up @@ -583,13 +584,54 @@ public void When_Primitive_DataTemplate()

SUT.ForceLoaded();

var inner = SUT.root.FindName("inner") as Windows.UI.Xaml.Controls.TextBlock;
var inner = SUT.root.FindName("inner") as TextBlock;

Assert.IsNull(inner.Text);

SUT.root.Content = "hello!";

Assert.AreEqual("hello!", inner.Text);
}

[TestMethod]
public void When_TypeMismatch()
{
var SUT = new Binding_TypeMismatch();

SUT.ForceLoaded();

var inner = SUT.FindName("mySlider") as Slider;

Assert.AreEqual(0.0, inner.Value);
Assert.AreEqual(0, SUT.MyInteger);

inner.Minimum = 10.0;

Assert.AreEqual(10.0, inner.Value);
Assert.AreEqual(10, SUT.MyInteger);
}

[TestMethod]
public void When_TypeMismatch_DataTemplate()
{
var SUT = new Binding_TypeMismatch_DataTemplate();

var rootData = new Binding_TypeMismatch_DataTemplate_Data();
SUT.root.Content = rootData;

Assert.AreEqual(0, rootData.MyInteger);

SUT.ForceLoaded();

var slider = SUT.FindName("mySlider") as Slider;

Assert.AreEqual(0.0, slider.Value);
Assert.AreEqual(0, rootData.MyInteger);

slider.Minimum = 10.0;

Assert.AreEqual(10.0, slider.Value);
Assert.AreEqual(10, rootData.MyInteger);
}
}
}

0 comments on commit b952235

Please sign in to comment.