Skip to content

Commit

Permalink
feat: Add support for x:Bind type cast syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Jan 27, 2021
1 parent f6372b4 commit c45407f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
4 changes: 3 additions & 1 deletion doc/articles/features/windows-ui-xaml-xbind.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,7 @@ Uno supports the [`x:Bind`](https://docs.microsoft.com/en-us/windows/uwp/xaml-pl
public void OnUncheckedRaised(object sender, RoutedEventArgs args) { }
```

# Not supported
- Type casts
```xaml
<TextBox FontFamily="{x:Bind (FontFamily)MyComboBox.SelectedValue}" />
```
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public class Given_XBindRewriter
[DataRow("ctx", "MyNameSpace.Static2.MyFunction(MyProperty)", "MyNameSpace.Static2.MyFunction(ctx.MyProperty)")]
[DataRow("ctx", "MyFunction(MyProperty)", "ctx.MyFunction(ctx.MyProperty)")]
[DataRow("ctx", "", "ctx")]
[DataRow("ctx", "(FontFamily)a.Value", "(FontFamily)ctx.a.Value")]
[DataRow("ctx", "(global::System.Int32)a.Value", "(global::System.Int32)ctx.a.Value")]

// Not supported https://github.com/unoplatform/uno/issues/5061
// [DataRow("ctx", "MyFunction((global::System.Int32)MyProperty)", "ctx.MyFunction((global::System.Int32)ctx.MyProperty)")]

// Main class (without context)
[DataRow("", "MyProperty.A", "MyProperty.A")]
Expand All @@ -38,6 +43,8 @@ public class Given_XBindRewriter
[DataRow("", "Static.MyFunction(MyProperty)", "Static.MyFunction(MyProperty)")]
[DataRow("", "MyNameSpace.Static2.MyFunction(MyProperty)", "MyNameSpace.Static2.MyFunction(MyProperty)")]
[DataRow("", "MyFunction(MyProperty)", "MyFunction(MyProperty)")]
[DataRow("", "(FontFamily)MyProperty", "(FontFamily)MyProperty")]
[DataRow("", "(FontFamily)MyProperty.A", "(FontFamily)MyProperty.A")]
public void When_PathRewrite(string contextName, string inputExpression, string expectedOutput)
{
bool IsStaticMethod(string name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ public override SyntaxNode VisitMemberAccessExpression(MemberAccessExpressionSyn

public override SyntaxNode VisitIdentifierName(IdentifierNameSyntax node)
{
var isValidParent = !Helpers.IsInsideMethod(node).result && !Helpers.IsInsideMemberAccessExpression(node).result;
var isInsideCast = Helpers.IsInsideCast(node);
var isValidParent = !Helpers.IsInsideMethod(node).result
&& !Helpers.IsInsideMemberAccessExpression(node).result
&& !isInsideCast.result;

if (isValidParent && !_isStaticMember(node.ToFullString()))
{
Expand Down Expand Up @@ -236,6 +239,24 @@ internal static (bool result, InvocationExpressionSyntax expression) IsInsideMet

return (false, null);
}

internal static (bool result, CastExpressionSyntax expression) IsInsideCast(SyntaxNode node)
{
var currentNode = node.Parent;

do
{
if (currentNode is CastExpressionSyntax cast)
{
return (true, cast);
}

currentNode = currentNode.Parent;
}
while (currentNode != null);

return (false, null);
}
}
}
}

0 comments on commit c45407f

Please sign in to comment.