Skip to content

Commit

Permalink
Merge pull request #1699 from pictos/pj/use-maui-converters
Browse files Browse the repository at this point in the history
Prefer to use Maui's converter.
  • Loading branch information
nickrandolph authored Jul 28, 2023
2 parents ae5abb6 + 016a5cd commit d0c9ef4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,43 +1,52 @@
<Page
x:Class="MauiEmbedding.Presentation.MauiColorsPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:embed="using:Uno.Extensions.Maui"
xmlns:maui="using:Microsoft.Maui.Controls"
xmlns:local="using:MauiEmbedding.Presentation"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:utu="using:Uno.Toolkit.UI"
Padding="20"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">
x:Class="MauiEmbedding.Presentation.MauiColorsPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:embed="using:Uno.Extensions.Maui"
xmlns:local="using:MauiEmbedding.Presentation"
xmlns:maui="using:Microsoft.Maui.Controls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:utu="using:Uno.Toolkit.UI"
Padding="20"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">

<Page.Resources>
<ResourceDictionary>
<x:String x:Key="HelloWorld">Hello from WinUI Resources</x:String>
<Color x:Key="DemoColor">#169420</Color>
<local:TitleConverter x:Key="TitleConverter" />
</ResourceDictionary>
</Page.Resources>
<Page.Resources>
<ResourceDictionary>
<x:String x:Key="HelloWorld">Hello from WinUI Resources</x:String>
<Color x:Key="DemoColor">#169420</Color>
<local:TitleConverter x:Key="TitleConverter" />
</ResourceDictionary>
</Page.Resources>


<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<utu:NavigationBar Content="{Binding Title}" />
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<utu:NavigationBar Content="{Binding Title}" />

<StackPanel Grid.Row="1">
<embed:MauiHost>
<maui:VerticalStackLayout Spacing="10">
<maui:Label Text="This text should be Red" TextColor="{embed:MauiColor Value='#FF0000'}" />
<maui:Label x:Name="lbl" Text="This text should be Fuchsia (set on code-behind)" />
<maui:Label BackgroundColor="{embed:MauiColor Value='#FFC0CB'}" Text="This background should be Pink" />
<maui:Label Text="This TextColor should be Purple, using named color" TextColor="{embed:MauiColor Value='Purple'}" />
<maui:Label Text="{embed:MauiResource Key=HelloWorld}" TextColor="{embed:MauiResource Key=DemoColor}"/>
</maui:VerticalStackLayout>
</embed:MauiHost>
</StackPanel>
</Grid>
<StackPanel Grid.Row="1">
<embed:MauiHost>
<maui:VerticalStackLayout Spacing="10">
<maui:Label
Margin="{embed:MauiThickness Value='0'}"
Text="This text should be Red"
TextColor="{embed:MauiColor Value='#FF0000'}" />
<maui:Label
x:Name="lbl"
Margin="{embed:MauiThickness Value='0,10'}"
Text="This text should be Fuchsia (set on code-behind)" />
<maui:Label
Margin="{embed:MauiThickness Value='0,10,0,10'}"
BackgroundColor="{embed:MauiColor Value='#FFC0CB'}"
Text="This background should be Pink" />
<maui:Label Text="This TextColor should be Purple, using named color" TextColor="{embed:MauiColor Value='Purple'}" />
<maui:Label Text="{embed:MauiResource Key=HelloWorld}" TextColor="{embed:MauiResource Key=DemoColor}" />
</maui:VerticalStackLayout>
</embed:MauiHost>
</StackPanel>
</Grid>
</Page>
20 changes: 3 additions & 17 deletions src/Uno.Extensions.Maui.UI/MauiThickness.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class MauiThickness : MarkupExtension


#if MAUI_EMBEDDING
static readonly Microsoft.Maui.Converters.ThicknessTypeConverter mauiThicknessConverter = new();

/// <inheritdoc />
protected override object ProvideValue()
{
Expand All @@ -21,23 +23,7 @@ protected override object ProvideValue()
return global::Microsoft.Maui.Thickness.Zero;
}

var temp = Value.Split(',')
.Select(x => x.Trim())
.Select(x => double.TryParse(x, out var thickness) ? (object)thickness : x);

var values = temp.OfType<double>().ToArray();
if (temp.Count() != values.Length)
{
throw new MauiEmbeddingException($"Unable to parse the Thickness string '{Value}'.");
}

return values.Length switch
{
1 => new Microsoft.Maui.Thickness(values[0]),
2 => new Microsoft.Maui.Thickness(values[0], values[1]),
4 => new Microsoft.Maui.Thickness(values[0], values[1], values[2], values[3]),
_ => throw new MauiEmbeddingException($"The Thickness string '{Value}' has an invalid number of arguments")
};
return mauiThicknessConverter.ConvertFrom(null, CultureInfo.InvariantCulture, Value);
}
#endif
}

0 comments on commit d0c9ef4

Please sign in to comment.