Skip to content

Commit

Permalink
[XC] Compile bindings with Source (#20610)
Browse files Browse the repository at this point in the history
* Remove restriction that prevents compiling bindings with Source property

* Update tests

* Fix bindings with mismatch between Source type and x:DataType
  • Loading branch information
simonrozsival committed Mar 11, 2024
1 parent 89fa80c commit 160a484
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://schemas.microsoft.com/dotnet/2021/maui/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Microsoft.Maui.Controls.ControlGallery.Issues"
mc:Ignorable="d"
x:Class="Microsoft.Maui.Controls.ControlGallery.Issues.Issue11764"
x:Name="Issue11764Page"
Expand All @@ -26,13 +27,13 @@
BackgroundColor="Red">
<SwipeView.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding BindingContext.TapCommand, Source={x:Reference Issue11764Page}}"
Command="{Binding TapCommand, Source={x:Reference Issue11764Page}, x:DataType=local:Issue11764}"
CommandParameter="{Binding}" />
</SwipeView.GestureRecognizers>
<SwipeView.RightItems>
<SwipeItems>
<SwipeItemView
Command="{Binding BindingContext.DeleteCommand, Source={x:Reference Issue11764Page}}"
Command="{Binding DeleteCommand, Source={x:Reference Issue11764Page}, x:DataType=local:Issue11764}"
CommandParameter="{Binding}">
<Grid
BackgroundColor="LightSlateGray">
Expand All @@ -58,7 +59,7 @@
</Grid>
</SwipeItemView>
<SwipeItemView
Command="{Binding BindingContext.PinCommand, Source={x:Reference Issue11764Page}}"
Command="{Binding PinCommand, Source={x:Reference Issue11764Page}, x:DataType=local:Issue11764}"
CommandParameter="{Binding}">
<Grid
BackgroundColor="LightSlateGray">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://schemas.microsoft.com/dotnet/2021/maui/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Microsoft.Maui.Controls.ControlGallery.Issues"
mc:Ignorable="d"
x:Class="Microsoft.Maui.Controls.ControlGallery.Issues.Issue11831"
x:Name="Issue11831Page"
Expand Down Expand Up @@ -54,7 +55,7 @@
<SwipeView.RightItems>
<SwipeItems>
<SwipeItemView
Command="{Binding BindingContext.DeleteCommand, Source={x:Reference Issue11831Page}}"
Command="{Binding DeleteCommand, Source={x:Reference Issue11831Page}, x:DataType=local:Issue11831}"
CommandParameter="{Binding}">
<Grid
BackgroundColor="LightSlateGray">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<SwipeView.RightItems>
<SwipeItems>
<SwipeItem
Command="{Binding Source={x:Reference Issue9417Page}, Path=BindingContext.DeleteCommand}"
Command="{Binding DeleteCommand, Source={x:Reference Issue9417Page}, x:DataType=local:Issue9417}"
CommandParameter="{Binding .}"
Text="Delete"
IconImageSource="{StaticResource TrashCanImage}"
Expand All @@ -50,7 +50,7 @@
<SwipeView.LeftItems>
<SwipeItems>
<SwipeItem
Command="{Binding Source={x:Reference Issue9417Page}, Path=BindingContext.EditCommand}"
Command="{Binding EditCommand, Source={x:Reference Issue9417Page}, x:DataType=local:Issue9417}"
CommandParameter="{Binding .}"
Text="Edit"
IconImageSource="{StaticResource EditImage}"
Expand Down
1 change: 0 additions & 1 deletion src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ internal static string GetContentProperty(XamlCache cache, TypeReference typeRef
context.LoggingHelper.LogWarningOrError(BuildExceptionCode.UnattributedMarkupType, context.XamlFilePath, node.LineNumber, node.LinePosition, 0, 0, vardefref.VariableDefinition.VariableType);

if (vardefref.VariableDefinition.VariableType.FullName == "Microsoft.Maui.Controls.Xaml.BindingExtension"
&& (node.Properties == null || !node.Properties.ContainsKey(new XmlName("", "Source"))) //do not compile bindings if Source is set
&& bpRef != null //do not compile bindings if we're not gonna SetBinding
)
foreach (var instruction in CompileBindingPath(node, context, vardefref.VariableDefinition))
Expand Down
2 changes: 2 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/BindingsCompiler.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
<Label Text="{Binding Text, Mode=OneTime}" x:Name="label8" />
<Label Text="{Binding StructModel.Text, Mode=TwoWay}" x:Name="label9" />
<Label Text="{Binding StructModel.Model.Text, Mode=TwoWay}" x:Name="label10" />
<Label Text="Text for label12" x:Name="label11" />
<Label Text="{Binding Text, x:DataType=Label, Source={x:Reference label11}}" x:Name="label12" />
</cmp:StackLayout>
<Label Text="{Binding Text}" x:Name="labelWithUncompiledBinding" />
</cmp:StackLayout>
Expand Down
3 changes: 3 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/BindingsCompiler.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ public void Test(bool useCompiledXaml)
//testing invalid bindingcontext type
layout.BindingContext = new object();
Assert.AreEqual(null, layout.label0.Text);

//testing source
Assert.That(layout.label12.Text, Is.EqualTo("Text for label12"));
}
}
}
Expand Down
14 changes: 12 additions & 2 deletions src/Controls/tests/Xaml.UnitTests/Issues/Gh3606.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Microsoft.Maui.Controls.Xaml.UnitTests
{
[XamlCompilation(XamlCompilationOptions.Skip)]
public partial class Gh3606 : ContentPage
{
public Gh3606()
Expand All @@ -28,9 +29,18 @@ class Tests
[TearDown] public void TearDown() => DispatcherProvider.SetCurrent(null);

[TestCase(true)]
public void BindingsWithSourceArentCompiled(bool useCompiledXaml)
[TestCase(false)]
public void BindingsWithSourceAreCompiled(bool useCompiledXaml)
{
new Gh3606(useCompiledXaml);
if (useCompiledXaml)
{
// The XAML file contains a mismatch between the source the x:DataType attribute so the compilation of the binding will fail
Assert.Throws(new BuildExceptionConstraint(4, 16), () => MockCompiler.Compile(typeof(Gh3606)));
}
else
{
Assert.DoesNotThrow(() => new Gh3606(useCompiledXaml: false));
}
}
}
}
Expand Down

0 comments on commit 160a484

Please sign in to comment.