Skip to content

Commit

Permalink
Merge pull request #5925 from unoplatform/dev/jela/xbind-default-data…
Browse files Browse the repository at this point in the history
…template

Propagate x:DefaultBindMode to DataTemplate definitions
  • Loading branch information
jeromelaban authored May 5, 2021
2 parents 1d2f029 + b92b42b commit e24219d
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ namespace Uno.UI.SourceGenerators.XamlGenerator
{
internal class Subclass
{
public Subclass(XamlMemberDefinition contentOwner, string returnType)
public Subclass(XamlMemberDefinition contentOwner, string returnType, string defaultBindMode)
{
ContentOwner = contentOwner;
ReturnType = returnType;
DefaultBindMode = defaultBindMode;
}


public XamlMemberDefinition ContentOwner { get;}

public string ReturnType { get; }

public string DefaultBindMode { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -721,44 +721,47 @@ private void BuildChildSubclasses(IIndentedStringBuilder writer, bool isTopLevel
var className = kvp.Key;
var contentOwner = kvp.Value.ContentOwner;

using (Scope(ns, className))
using (TrySetDefaultBindMode(contentOwner.Owner, kvp.Value.DefaultBindMode))
{
var classAccessibility = isTopLevel ? "" : "private";

using (writer.BlockInvariant($"{classAccessibility} class {className}"))
using (Scope(ns, className))
{
using (ResourceOwnerScope())
var classAccessibility = isTopLevel ? "" : "private";

using (writer.BlockInvariant($"{classAccessibility} class {className}"))
{
using (writer.BlockInvariant($"public {kvp.Value.ReturnType} Build(object {CurrentResourceOwner?.Name})"))
using (ResourceOwnerScope())
{
writer.AppendLineInvariant("var nameScope = new global::Windows.UI.Xaml.NameScope();");
writer.AppendLineInvariant($"{kvp.Value.ReturnType} __rootInstance = null;");
writer.AppendLineInvariant("__rootInstance = ");
using (writer.BlockInvariant($"public {kvp.Value.ReturnType} Build(object {CurrentResourceOwner?.Name})"))
{
writer.AppendLineInvariant("var nameScope = new global::Windows.UI.Xaml.NameScope();");
writer.AppendLineInvariant($"{kvp.Value.ReturnType} __rootInstance = null;");
writer.AppendLineInvariant("__rootInstance = ");

// Is never considered in Global Resources because class encapsulation
BuildChild(writer, contentOwner, contentOwner.Objects.First());
// Is never considered in Global Resources because class encapsulation
BuildChild(writer, contentOwner, contentOwner.Objects.First());

writer.AppendLineInvariant(";");
writer.AppendLineInvariant(";");

BuildCompiledBindingsInitializerForTemplate(writer);
BuildCompiledBindingsInitializerForTemplate(writer);

using (writer.BlockInvariant("if (__rootInstance is DependencyObject d)", kvp.Value.ReturnType))
{
writer.AppendLineInvariant("global::Windows.UI.Xaml.NameScope.SetNameScope(d, nameScope);");
writer.AppendLineInvariant("global::Uno.UI.FrameworkElementHelper.AddObjectReference(d, this);");
}
using (writer.BlockInvariant("if (__rootInstance is DependencyObject d)", kvp.Value.ReturnType))
{
writer.AppendLineInvariant("global::Windows.UI.Xaml.NameScope.SetNameScope(d, nameScope);");
writer.AppendLineInvariant("global::Uno.UI.FrameworkElementHelper.AddObjectReference(d, this);");
}

writer.AppendLineInvariant("return __rootInstance;");
writer.AppendLineInvariant("return __rootInstance;");
}
}
}

BuildComponentFields(writer);

TryBuildElementStubHolders(writer);
TryBuildElementStubHolders(writer);

BuildBackingFields(writer);
BuildBackingFields(writer);

BuildChildSubclasses(writer);
BuildChildSubclasses(writer);
}
}
}
}
Expand Down Expand Up @@ -3436,7 +3439,7 @@ private void RegisterBackingField(string type, string name, Accessibility access

private void RegisterChildSubclass(string name, XamlMemberDefinition owner, string returnType)
{
CurrentScope.Subclasses[name] = new Subclass(owner, returnType);
CurrentScope.Subclasses[name] = new Subclass(owner, returnType, GetDefaultBindMode());
}

private void BuildComplexPropertyValue(IIndentedStringBuilder writer, XamlMemberDefinition member, string? prefix, string? closureName = null, bool generateAssignation = true)
Expand Down Expand Up @@ -5391,9 +5394,12 @@ XamlMemberDefinition GenerateBinding(string name, XamlMemberDefinition? memberDe
/// <summary>
/// Set the active DefaultBindMode, if x:DefaultBindMode is defined on this <paramref name="xamlObjectDefinition"/>.
/// </summary>
private IDisposable? TrySetDefaultBindMode(XamlObjectDefinition xamlObjectDefinition)
private IDisposable? TrySetDefaultBindMode(XamlObjectDefinition? xamlObjectDefinition, string? ambientDefaultBindMode = null)
{
var definedMode = xamlObjectDefinition.Members.FirstOrDefault(m => m.Member.Name == "DefaultBindMode")?.Value?.ToString();
var definedMode = xamlObjectDefinition
?.Members
.FirstOrDefault(m => m.Member.Name == "DefaultBindMode")?.Value?.ToString()
?? ambientDefaultBindMode;

if (definedMode == null)
{
Expand Down
6 changes: 5 additions & 1 deletion src/Uno.UI.Tests/Uno.UI.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@
<UndefineProperties>TargetFramework</UndefineProperties>
</ProjectReference>
</ItemGroup>


<ItemGroup>
<UpToDateCheckInput Include="**\*.xaml" Exclude="bin\**\*.xaml;obj\**\*.xaml" />
</ItemGroup>

<ItemGroup>
<None Update="ResourceLoader\Strings\en\Resources.resw">
<Generator>ResXFileCodeGenerator</Generator>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<Page x:Class="Uno.UI.Tests.Windows_UI_Xaml_Data.xBindTests.Controls.Binding_DefaultBindMode_DataTemplate"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:Uno.UI.Tests.Windows_UI_Xaml_Data.xBindTests.Controls"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:DefaultBindMode="OneWay"
mc:Ignorable="d">

<Page.Resources>
<DataTemplate x:Key="myTemplate" x:DataType="local:Binding_DefaultBindMode_DataTemplate_Model">
<Grid>
<StackPanel>
<TextBlock x:Name="Default_undefined"
x:FieldModifier="public"
Text="{x:Bind Default_undefined_Property}" />
<TextBlock x:Name="Default_undefined_OneWay"
x:FieldModifier="public"
Text="{x:Bind Default_undefined_OneWay_Property, Mode=OneWay}" />
<TextBlock x:Name="Default_undefined_TwoWay"
x:FieldModifier="public"
Text="{x:Bind Default_undefined_TwoWay_Property, Mode=TwoWay}" />
</StackPanel>

<StackPanel x:DefaultBindMode="OneWay">
<TextBlock x:Name="Default_OneWay"
x:FieldModifier="public"
Text="{x:Bind Default_OneWay_Property}" />
<TextBlock x:Name="Default_OneWay_OneWay"
x:FieldModifier="public"
Text="{x:Bind Default_OneWay_OneWay_Property, Mode=OneWay}" />
<TextBlock x:Name="Default_OneWay_TwoWay"
x:FieldModifier="public"
Text="{x:Bind Default_OneWay_TwoWay_Property, Mode=TwoWay}" />
</StackPanel>

<StackPanel x:DefaultBindMode="TwoWay">
<TextBlock x:Name="Default_TwoWay"
x:FieldModifier="public"
Text="{x:Bind Default_TwoWay_Property}" />
<TextBlock x:Name="Default_TwoWay_OneWay"
x:FieldModifier="public"
Text="{x:Bind Default_TwoWay_OneWay_Property, Mode=OneWay}" />
<TextBlock x:Name="Default_TwoWay_TwoWay"
x:FieldModifier="public"
Text="{x:Bind Default_TwoWay_TwoWay_Property, Mode=TwoWay}" />
</StackPanel>

<StackPanel x:DefaultBindMode="TwoWay">
<TextBlock x:Name="Nested_Default_1"
x:FieldModifier="public"
Text="{x:Bind Nested_Default_1_Property}" />
<StackPanel x:DefaultBindMode="OneWay">
<TextBlock x:Name="Nested_Default_2"
x:FieldModifier="public"
Text="{x:Bind Nested_Default_2_Property}" />
<TextBlock x:Name="Nested_Default_OneWay_OneTime"
x:FieldModifier="public"
Text="{x:Bind Nested_Default_OneWay_OneTime_Property, Mode=OneTime}" />
<TextBlock x:Name="Nested_Default_OneWay_OneWay"
x:FieldModifier="public"
Text="{x:Bind Nested_Default_OneWay_OneWay_Property, Mode=OneWay}" />
<TextBlock x:Name="Nested_Default_OneWay_TwoWay"
x:FieldModifier="public"
Text="{x:Bind Nested_Default_OneWay_TwoWay_Property, Mode=TwoWay}" />
</StackPanel>
</StackPanel>
</Grid>

</DataTemplate>
</Page.Resources>

<ContentControl ContentTemplate="{StaticResource myTemplate}"
Content="{Binding}" />

</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
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_DefaultBindMode_DataTemplate : Page
{
public Binding_DefaultBindMode_DataTemplate()
{
this.InitializeComponent();
}
}

public partial class Binding_DefaultBindMode_DataTemplate_Model : DependencyObject
{
public string Default_undefined_Property
{
get { return (string)GetValue(Default_undefined_PropertyProperty); }
set { SetValue(Default_undefined_PropertyProperty, value); }
}

public static readonly DependencyProperty Default_undefined_PropertyProperty =
DependencyProperty.Register("Default_undefined_Property", typeof(string), typeof(Binding_DefaultBindMode_DataTemplate_Model), new FrameworkPropertyMetadata(null));

public string Default_undefined_OneWay_Property
{
get { return (string)GetValue(Default_undefined_OneWay_PropertyProperty); }
set { SetValue(Default_undefined_OneWay_PropertyProperty, value); }
}

public static readonly DependencyProperty Default_undefined_OneWay_PropertyProperty =
DependencyProperty.Register("Default_undefined_OneWay_Property", typeof(string), typeof(Binding_DefaultBindMode_DataTemplate_Model), new FrameworkPropertyMetadata(null));

public string Default_undefined_TwoWay_Property
{
get { return (string)GetValue(Default_undefined_TwoWay_PropertyProperty); }
set { SetValue(Default_undefined_TwoWay_PropertyProperty, value); }
}

public static readonly DependencyProperty Default_undefined_TwoWay_PropertyProperty =
DependencyProperty.Register("Default_undefined_TwoWay_Property", typeof(string), typeof(Binding_DefaultBindMode_DataTemplate_Model), new FrameworkPropertyMetadata(null));

public string Default_OneWay_Property
{
get { return (string)GetValue(Default_OneWay_PropertyProperty); }
set { SetValue(Default_OneWay_PropertyProperty, value); }
}

public static readonly DependencyProperty Default_OneWay_PropertyProperty =
DependencyProperty.Register("Default_OneWay_Property", typeof(string), typeof(Binding_DefaultBindMode_DataTemplate_Model), new FrameworkPropertyMetadata(null));

public string Default_OneWay_OneWay_Property
{
get { return (string)GetValue(Default_OneWay_OneWay_PropertyProperty); }
set { SetValue(Default_OneWay_OneWay_PropertyProperty, value); }
}

public static readonly DependencyProperty Default_OneWay_OneWay_PropertyProperty =
DependencyProperty.Register("Default_OneWay_OneWay_Property", typeof(string), typeof(Binding_DefaultBindMode_DataTemplate_Model), new FrameworkPropertyMetadata(null));


public string Default_OneWay_TwoWay_Property
{
get { return (string)GetValue(Default_OneWay_TwoWay_PropertyProperty); }
set { SetValue(Default_OneWay_TwoWay_PropertyProperty, value); }
}

public static readonly DependencyProperty Default_OneWay_TwoWay_PropertyProperty =
DependencyProperty.Register("Default_OneWay_TwoWay_Property", typeof(string), typeof(Binding_DefaultBindMode_DataTemplate_Model), new FrameworkPropertyMetadata(null));

public string Default_TwoWay_Property
{
get { return (string)GetValue(Default_TwoWay_PropertyProperty); }
set { SetValue(Default_TwoWay_PropertyProperty, value); }
}

public static readonly DependencyProperty Default_TwoWay_PropertyProperty =
DependencyProperty.Register("Default_TwoWay_Property", typeof(string), typeof(Binding_DefaultBindMode_DataTemplate_Model), new FrameworkPropertyMetadata(null));

public string Default_TwoWay_OneWay_Property
{
get { return (string)GetValue(Default_TwoWay_OneWay_PropertyProperty); }
set { SetValue(Default_TwoWay_OneWay_PropertyProperty, value); }
}

public static readonly DependencyProperty Default_TwoWay_OneWay_PropertyProperty =
DependencyProperty.Register("Default_TwoWay_OneWay_Property", typeof(string), typeof(Binding_DefaultBindMode_DataTemplate_Model), new FrameworkPropertyMetadata(null));

public string Default_TwoWay_TwoWay_Property
{
get { return (string)GetValue(Default_TwoWay_TwoWay_PropertyProperty); }
set { SetValue(Default_TwoWay_TwoWay_PropertyProperty, value); }
}

public static readonly DependencyProperty Default_TwoWay_TwoWay_PropertyProperty =
DependencyProperty.Register("Default_TwoWay_TwoWay_Property", typeof(string), typeof(Binding_DefaultBindMode_DataTemplate_Model), new FrameworkPropertyMetadata(null));

public string Nested_Default_1_Property
{
get { return (string)GetValue(Nested_Default_1_PropertyProperty); }
set { SetValue(Nested_Default_1_PropertyProperty, value); }
}

public static readonly DependencyProperty Nested_Default_1_PropertyProperty =
DependencyProperty.Register("Nested_Default_1_Property", typeof(string), typeof(Binding_DefaultBindMode_DataTemplate_Model), new FrameworkPropertyMetadata(null));

public string Nested_Default_2_Property
{
get { return (string)GetValue(Nested_Default_2_PropertyProperty); }
set { SetValue(Nested_Default_2_PropertyProperty, value); }
}

public static readonly DependencyProperty Nested_Default_2_PropertyProperty =
DependencyProperty.Register("Nested_Default_2_Property", typeof(string), typeof(Binding_DefaultBindMode_DataTemplate_Model), new FrameworkPropertyMetadata(null));

public string Nested_Default_OneWay_OneWay_Property
{
get { return (string)GetValue(Nested_Default_OneWay_OneWay_PropertyProperty); }
set { SetValue(Nested_Default_OneWay_OneWay_PropertyProperty, value); }
}

public static readonly DependencyProperty Nested_Default_OneWay_OneWay_PropertyProperty =
DependencyProperty.Register("Nested_Default_OneWay_OneWay_Property", typeof(string), typeof(Binding_DefaultBindMode_DataTemplate_Model), new FrameworkPropertyMetadata(null));

public string Nested_Default_OneWay_TwoWay_Property
{
get { return (string)GetValue(Nested_Default_OneWay_TwoWay_PropertyProperty); }
set { SetValue(Nested_Default_OneWay_TwoWay_PropertyProperty, value); }
}

public static readonly DependencyProperty Nested_Default_OneWay_TwoWay_PropertyProperty =
DependencyProperty.Register("Nested_Default_OneWay_TwoWay_Property", typeof(string), typeof(Binding_DefaultBindMode_DataTemplate_Model), new FrameworkPropertyMetadata(null));

public string Nested_Default_OneWay_OneTime_Property
{
get { return (string)GetValue(Nested_Default_OneWay_OneTime_PropertyProperty); }
set { SetValue(Nested_Default_OneWay_OneTime_PropertyProperty, value); }
}

public static readonly DependencyProperty Nested_Default_OneWay_OneTime_PropertyProperty =
DependencyProperty.Register("Nested_Default_OneWay_OneTime_Property", typeof(string), typeof(Binding_DefaultBindMode_DataTemplate_Model), new FrameworkPropertyMetadata(null));
}
}
Loading

0 comments on commit e24219d

Please sign in to comment.