Skip to content

Commit

Permalink
fix(XamlFileGenerator): XAML markup extension requires full name unop…
Browse files Browse the repository at this point in the history
  • Loading branch information
workgroupengineering committed Nov 10, 2020
1 parent e0a940e commit 488a556
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Runtime.CompilerServices;
using Uno.UI.Xaml;


namespace Uno.UI.SourceGenerators.XamlGenerator
{
internal partial class XamlFileGenerator
Expand Down Expand Up @@ -1678,20 +1679,27 @@ private bool HasDescendantsWithMarkupExtension(XamlObjectDefinition xamlObjectDe
return false;
}

private bool IsCustomMarkupExtensionType(XamlType xamlType)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private INamedTypeSymbol? GetMarkupExtensionType(XamlType xamlType)
{
if (xamlType == null)
{
return false;
return null;
}

// Adjustment for Uno.Xaml parser which returns the namespace in the name
var xamlTypeName = xamlType.Name.Contains(':') ? xamlType.Name.Split(':').LastOrDefault() : xamlType.Name;
var extendedXamlTypeName = xamlTypeName + "Extension";

// Determine if the type is a custom markup extension
return _markupExtensionTypes.Any(ns => ns.Name.Equals(xamlTypeName, StringComparison.InvariantCulture));
// return the type of custom markup extension
return _markupExtensionTypes.FirstOrDefault(ns => ns.Name.Equals(xamlTypeName, StringComparison.InvariantCulture)
|| ns.Name.Equals(extendedXamlTypeName, StringComparison.InvariantCulture));
}

private bool IsCustomMarkupExtensionType(XamlType xamlType) =>
// Determine if the type is a custom markup extension
GetMarkupExtensionType(xamlType) != null;

private bool IsXamlTypeConverter(INamedTypeSymbol symbol)
{
return _xamlConversionTypes.Any(ns => ns.Equals(symbol));
Expand Down Expand Up @@ -3604,24 +3612,25 @@ private string GetCustomMarkupExtensionValue(XamlMemberDefinition member)
var markupTypeDef = member
.Objects
.FirstOrDefault(o => IsCustomMarkupExtensionType(o.Type));

var markupType = GetMarkupExtensionType(markupTypeDef.Type);
// Build a string of all its properties
var properties = markupTypeDef
.Members
.Select(m =>
{
var resourceName = GetSimpleStaticResourceRetrieval(m);
var propertyType = markupType.GetAllPropertiesWithName(m.Member.Name)?
.FirstOrDefault()?.Type as INamedTypeSymbol;
var resourceName = GetSimpleStaticResourceRetrieval(m, propertyType);
var value = resourceName != null
? resourceName
: BuildLiteralValue(m, owner: member);
: BuildLiteralValue(m, propertyType: propertyType, owner: member);
return "{0} = {1}".InvariantCultureFormat(m.Member.Name, value);
})
.JoinBy(", ");

// Get the full globalized namespaces for the custom markup extension and also for IMarkupExtensionOverrides
var markupType = GetType(markupTypeDef.Type);

var markupTypeFullName = GetGlobalizedTypeName(markupType.GetFullName());
var xamlMarkupFullName = GetGlobalizedTypeName(XamlConstants.Types.IMarkupExtensionOverrides);

Expand Down Expand Up @@ -4106,6 +4115,8 @@ string Inner()

propertyType = propertyType ?? FindPropertyType(member.Member);



if (propertyType != null)
{
var s = BuildLiteralValue(propertyType, memberValue, owner, member.Member.Name, objectUid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@

<!-- Make sure custom objects can be returned from a markup extension without a return type specified -->
<TextBlock DataContext="{ext:NoReturnTypeMarkupExt UseValue=UseValue1, Value1={StaticResource TestObject}}" />

<!--XAML markup extension without Suffix Extension-->
<TextBlock Text="{ext:TestMarkupSuffix}"/>

<!--XAML markup extension without Suffix Extension work on binding-->
<TextBlock Text="{Binding IsRightTapEnabled, Converter={ext:TestMarkupSuffix}}"/>

<!-- XAML markup extension without Suffix Extension work on bindin an attached property using a binding and converter -->
<TextBlock ext:MarkupExtensionTestBehavior.CustomText="{Binding IsRightTapEnabled, ElementName=RootGrid, Converter={ext:TestMarkupSuffix}}" />


</StackPanel>
</Grid>
</UserControl>
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ protected override object ProvideValue()
}
}

public class TestMarkupSuffixExtension : MarkupExtension
{
protected override object ProvideValue()
{
return null;
}
}

public class ComplexObject
{
public string StringProp { get; set; }
Expand Down
11 changes: 11 additions & 0 deletions src/Uno.UI.Tests/App/Xaml/Test_MarkupExtension.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<UserControl.Resources>
<x:String x:Key="ResourceString">From a Resource String</x:String>
<x:String x:Key="ResourceStringFull">From a Resource String FullName</x:String>
<ex:TestEntityObject x:Key="TestEntity" />
</UserControl.Resources>

Expand Down Expand Up @@ -47,6 +48,16 @@

<TextBlock x:Name="Text10"
DataContext="{ex:NoReturnTypeMarkupExt UseValue=UseValue1, Value1={StaticResource TestEntity}}" />

<TextBlock x:Name="Text11"
Text="{ex:SimpleMarkupExtExtension TextValue='Just a simple ... fullname'}" />

<TextBlock x:Name="Text12"
Text="{ex:SimpleMarkupExtExtension TextValue={StaticResource ResourceStringFull}}" />

<TextBlock x:Name="Text13"
beh:AttachedPropertiesBehavior.CustomText="{ex:SimpleMarkupExtExtension TextValue='String from attached property Fullname'}" />

</StackPanel>

</UserControl>
3 changes: 3 additions & 0 deletions src/Uno.UI.Tests/App/Xaml/Test_MarkupExtension.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public sealed partial class Test_MarkupExtension : UserControl
public TextBlock TestText8 => Text8;
public TextBlock TestText9 => Text9;
public TextBlock TestText10 => Text10;
public TextBlock TestText11 => Text11;
public TextBlock TestText12 => Text12;
public TextBlock TestText13 => Text13;

public Test_MarkupExtension()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public void When_ExtensionProvidesValues()
Assert.AreEqual(22.0, control.TestText9.FontSize);
Assert.AreEqual(3, control.TestText9.MaxLines);
Assert.IsInstanceOfType(control.TestText10.DataContext, typeof(TestEntityObject));
Assert.AreEqual("Just a simple ... fullname markup extension", control.TestText11.Text);
Assert.AreEqual("From a Resource String FullName markup extension", control.TestText12.Text);
Assert.AreEqual("String from attached property Fullname markup extension", control.TestText13.Text);
}
finally
{
Expand All @@ -47,7 +50,7 @@ public void When_ExtensionProvidesValues()
}

[MarkupExtensionReturnType(ReturnType = typeof(string))]
public class SimpleMarkupExt : Windows.UI.Xaml.Markup.MarkupExtension
public class SimpleMarkupExtExtension : Windows.UI.Xaml.Markup.MarkupExtension
{
public string TextValue { get; set; }

Expand Down

0 comments on commit 488a556

Please sign in to comment.