Skip to content

Commit

Permalink
feat: Add support for multiple XamlReader types conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Nov 18, 2021
1 parent 6105d1f commit 8c4845d
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Windows.UI.Xaml.Controls.Primitives;
using Microsoft.Extensions.Logging;
using Microsoft.UI;
using Windows.UI;

namespace Uno.UI.Tests.Windows_UI_Xaml_Markup.XamlReaderTests
{
Expand Down Expand Up @@ -796,10 +797,23 @@ public void When_StateTrigger_PropertyPath()
}

[TestMethod]
public void When_Brush_And_StringColor()
public void When_Color_Thickness_GridLength_As_String()
{
var s = GetContent(nameof(When_Brush_And_StringColor));
var s = GetContent(nameof(When_Color_Thickness_GridLength_As_String));
var r = Windows.UI.Xaml.Markup.XamlReader.Load(s) as ContentControl;

Assert.AreEqual(Windows.UI.Colors.Red, r.Resources["Color01"]);
Assert.AreEqual(Windows.UI.Colors.Blue, (r.Resources["scb01"] as SolidColorBrush).Color);
Assert.AreEqual(new Thickness(42), r.Resources["thickness"]);
Assert.AreEqual(new CornerRadius(42), r.Resources["cornerRadius"]);
Assert.AreEqual("TestFamily", (r.Resources["fontFamily"] as FontFamily).Source);
Assert.AreEqual(GridLength.FromString("42"), r.Resources["gridLength"]);
Assert.AreEqual(Windows.UI.Xaml.Media.Animation.KeyTime.FromTimeSpan(TimeSpan.Parse("1:2:3")), r.Resources["keyTime"]);
Assert.AreEqual(new Duration(TimeSpan.Parse("1:2:3")), r.Resources["duration"]);
Assert.AreEqual(Matrix.Identity, r.Resources["matrix"]);
Assert.AreEqual(Windows.UI.Text.FontWeights.Bold, r.Resources["fontWeight"]);

Assert.AreEqual(Windows.UI.Colors.Red, ((r.Content as Grid)?.Background as SolidColorBrush).Color);
}

[TestMethod]
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="testPage"
Width="42">
<UserControl.Resources>
<Color x:Key="Color01">Red</Color>
<SolidColorBrush x:Key="scb01">Blue</SolidColorBrush>
<Thickness x:Key="thickness">42</Thickness>
<CornerRadius x:Key="cornerRadius">42</CornerRadius>
<FontFamily x:Key="fontFamily">TestFamily</FontFamily>
<GridLength x:Key="gridLength">42</GridLength>
<KeyTime x:Key="keyTime">1:2:3</KeyTime>
<Duration x:Key="duration">1:2:3</Duration>
<Matrix x:Key="matrix">1,0,0,1,0,0</Matrix>
<FontWeight x:Key="fontWeight">Bold</FontWeight>
</UserControl.Resources>
<Grid>
<Grid.Background>
<SolidColorBrush>
<SolidColorBrush.Color>
<Color>Red</Color>
</SolidColorBrush.Color>
</SolidColorBrush>
</Grid.Background>
</Grid>
</UserControl>
5 changes: 4 additions & 1 deletion src/Uno.UI/UI/Xaml/Markup/Reader/XamlConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public static class Namespaces
public const string Primitives = Controls + ".Primitives";
public const string Text = RootWUINamespace + ".Text";
public const string Data = BaseXamlNamespace + ".Data";
public const string XamlText = BaseXamlNamespace + ".Text";
public const string Documents = BaseXamlNamespace + ".Documents";
public const string Media = BaseXamlNamespace + ".Media";
public const string MediaAnimation = BaseXamlNamespace + ".Media.Animation";
Expand All @@ -43,8 +44,9 @@ public static class Namespaces
MediaAnimation,
RootWUINamespace,
BaseXamlNamespace,
Data,
Text,
Documents,
XamlText,
"System",
};

Expand All @@ -60,6 +62,7 @@ public static class Namespaces
MediaAnimation,
Shapes,
Text,
XamlText,
};
}

Expand Down
24 changes: 22 additions & 2 deletions src/Uno.UI/UI/Xaml/Markup/Reader/XamlObjectBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI;
using Windows.Foundation;
using Windows.UI.Text;

#if XAMARIN_ANDROID
using _View = Android.Views.View;
Expand All @@ -33,6 +36,21 @@ internal class XamlObjectBuilder
private Queue<Action> _postActions = new Queue<Action>();
private static readonly Regex _attachedPropertMatch = new Regex(@"(\(.*?\))");

private static Type[] _genericConvertibles = new []
{
typeof(Media.Brush),
typeof(Media.SolidColorBrush),
typeof(Color),
typeof(Thickness),
typeof(CornerRadius),
typeof(Media.FontFamily),
typeof(GridLength),
typeof(Media.Animation.KeyTime),
typeof(Duration),
typeof(Media.Matrix),
typeof(FontWeight),
};

public XamlObjectBuilder(XamlFileDefinition xamlFileDefinition)
{
_fileDefinition = xamlFileDefinition;
Expand Down Expand Up @@ -110,9 +128,11 @@ private object LoadObject(XamlObjectDefinition control)
{
return stringValue;
}
else if (type == typeof(Media.Brush) && control.Members.Where(m => m.Member.Name == "_UnknownContent").FirstOrDefault()?.Value is string brushStringValue)
else if (
_genericConvertibles.Contains(type)
&& control.Members.Where(m => m.Member.Name == "_UnknownContent").FirstOrDefault()?.Value is string otherContentValue)
{
return XamlBindingHelper.ConvertValue(typeof(Media.Brush), brushStringValue);
return XamlBindingHelper.ConvertValue(type, otherContentValue);
}
else
{
Expand Down
31 changes: 20 additions & 11 deletions src/Uno.UI/UI/Xaml/Markup/Reader/XamlTypeResolver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


using System;
using System.Collections.Generic;
using System.Collections.Immutable;
Expand All @@ -8,14 +8,20 @@
using Uno;
using Uno.Extensions;
using Uno.Xaml;
using Windows.UI;
using Windows.Foundation;

namespace Windows.UI.Xaml.Markup.Reader
{
internal class XamlTypeResolver
{
private readonly static Assembly _frameworkElementAssembly = typeof(FrameworkElement).Assembly;

private readonly Func<string, Type> _findType;
internal class XamlTypeResolver
{
private readonly static Assembly[] _lookupAssemblies = new[]{
typeof(FrameworkElement).Assembly,
typeof(Color).Assembly,
typeof(Size).Assembly,
};

private readonly Func<string, Type> _findType;
private readonly Func<Type, string, bool> _isAttachedProperty;
private readonly XamlFileDefinition FileDefinition;
private readonly Func<string, string, Type> _findPropertyTypeByName;
Expand Down Expand Up @@ -330,12 +336,15 @@ private Type SourceFindType(string name)
// This lookup is performed in the current assembly as it is the
// original behavior, and the Wasm AOT engine does not yet respect this
// behavior (because of Wasm missing stack walking feature)
var type = _frameworkElementAssembly.GetType(clrNamespace + "." + name);
foreach (var assembly in _lookupAssemblies)
{
var type = assembly.GetType(clrNamespace + "." + name);

if (type != null)
{
return type;
}
if (type != null)
{
return type;
}
}
}
}

Expand Down

0 comments on commit 8c4845d

Please sign in to comment.