Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit da3c552

Browse files
Adding converter samples (#954)
* first 5 converters * added inverted bool converter * added Is null or empty Converter * added Is not null or empty Converter * added ListIsNotNullOrEmptyConverter * added ListToStringConverter * added NotEqualConverter * Text case Converter * removed nameof from property changed calls * Updated the description page / unified set property in the vms * Update samples/XCT.Sample/ViewModels/Converters/ConvertersGalleryViewModel.cs * Update samples/XCT.Sample/ViewModels/Converters/IsNullOrEmptyConverterViewModel.cs Co-authored-by: Andrei <[email protected]>
1 parent 14ad237 commit da3c552

30 files changed

+668
-14
lines changed

samples/XCT.Sample.iOS/Main.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
5-
using Foundation;
6-
using UIKit;
1+
using UIKit;
72

83
namespace Xamarin.CommunityToolkit.Sample.iOS
94
{
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<pages:BasePage
3+
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Converters.BoolToObjectConverterPage"
4+
xmlns="http://xamarin.com/schemas/2014/forms"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:converters="clr-namespace:Xamarin.CommunityToolkit.Converters;assembly=Xamarin.CommunityToolkit"
7+
xmlns:pages="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages"
8+
BackgroundColor="{StaticResource AppBackgroundColor}">
9+
<ContentPage.Resources>
10+
<ResourceDictionary>
11+
<converters:BoolToObjectConverter
12+
x:Key="BoolToObjectConverter"
13+
FalseObject="#0000FF"
14+
TrueObject="#FF0000" />
15+
</ResourceDictionary>
16+
</ContentPage.Resources>
17+
<ContentPage.Content>
18+
<StackLayout
19+
Padding="10,10"
20+
HorizontalOptions="Fill"
21+
Spacing="10"
22+
VerticalOptions="Fill">
23+
<Label Text="The BoolToObjectConverter is a converter that allows users to convert a bool value binding to a specific object. By providing both a TrueObject and a FalseObject in the converter the appropriate object will be used depending on the value of the binding." TextColor="{StaticResource NormalLabelTextColor}" />
24+
<StackLayout
25+
HorizontalOptions="CenterAndExpand"
26+
Orientation="Horizontal"
27+
Spacing="10">
28+
<Label Text="Blue" TextColor="{StaticResource NormalLabelTextColor}" />
29+
<Switch x:Name="ColorToggle" IsToggled="False" />
30+
<Label Text="Red" TextColor="{StaticResource NormalLabelTextColor}" />
31+
</StackLayout>
32+
<BoxView
33+
BindingContext="{x:Reference Name=ColorToggle}"
34+
HeightRequest="200"
35+
WidthRequest="200"
36+
Color="{Binding Path=IsToggled, Converter={StaticResource BoolToObjectConverter}}" />
37+
</StackLayout>
38+
</ContentPage.Content>
39+
</pages:BasePage>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
using Xamarin.Forms;
5+
6+
namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
7+
{
8+
public partial class BoolToObjectConverterPage
9+
{
10+
public BoolToObjectConverterPage()
11+
{
12+
InitializeComponent();
13+
}
14+
}
15+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<pages:BasePage
3+
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Converters.DoubleToIntConverterPage"
4+
xmlns="http://xamarin.com/schemas/2014/forms"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:pages="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages"
7+
xmlns:vm="clr-namespace:Xamarin.CommunityToolkit.Sample.ViewModels.Converters"
8+
xmlns:xct="clr-namespace:Xamarin.CommunityToolkit.Converters;assembly=Xamarin.CommunityToolkit"
9+
BackgroundColor="{StaticResource AppBackgroundColor}">
10+
11+
<ContentPage.BindingContext>
12+
<vm:DoubleToIntConverterViewModel />
13+
</ContentPage.BindingContext>
14+
<ContentPage.Resources>
15+
<ResourceDictionary>
16+
<xct:DoubleToIntConverter x:Key="DoubleToIntConverter" />
17+
</ResourceDictionary>
18+
</ContentPage.Resources>
19+
<ContentPage.Content>
20+
<StackLayout
21+
Padding="10,10"
22+
HorizontalOptions="Fill"
23+
Spacing="10"
24+
VerticalOptions="Fill">
25+
<Label Text="The DoubleToIntConverter is a converter that allows users to convert an incoming double value to an int. Optionally the user can provide a multiplier to the conversion through the Ratio property." TextColor="{StaticResource NormalLabelTextColor}" />
26+
<Label Text="Please enter a decimal Number" TextColor="{StaticResource NormalLabelTextColor}" />
27+
<Entry
28+
x:Name="ExampleText"
29+
Placeholder="Example 2.3"
30+
Text="{Binding Input}"
31+
TextColor="{StaticResource NormalLabelTextColor}" />
32+
<Label
33+
Padding="7,0,0,0"
34+
Text="{Binding Path=Input, Converter={StaticResource DoubleToIntConverter}, ConverterParameter=1}"
35+
TextColor="{StaticResource NormalLabelTextColor}" />
36+
</StackLayout>
37+
</ContentPage.Content>
38+
</pages:BasePage>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
using Xamarin.Forms;
5+
6+
namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
7+
{
8+
public partial class DoubleToIntConverterPage
9+
{
10+
public DoubleToIntConverterPage()
11+
{
12+
InitializeComponent();
13+
}
14+
}
15+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<pages:BasePage
3+
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Converters.EqualConverterPage"
4+
xmlns="http://xamarin.com/schemas/2014/forms"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:pages="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages"
7+
xmlns:xct="clr-namespace:Xamarin.CommunityToolkit.Converters;assembly=Xamarin.CommunityToolkit"
8+
BackgroundColor="{StaticResource AppBackgroundColor}">
9+
<ContentPage.Resources>
10+
<ResourceDictionary>
11+
<xct:EqualConverter x:Key="EqualConverter" />
12+
</ResourceDictionary>
13+
</ContentPage.Resources>
14+
<ContentPage.Content>
15+
<StackLayout
16+
Padding="10,10"
17+
HorizontalOptions="Fill"
18+
Spacing="10"
19+
VerticalOptions="Fill">
20+
<Label Text="The EqualConverter is a converter that allows users to convert any value binding to a boolean depending on whether or not it is equal to a different value. The initial binding contains the object that will be compared and the ConverterParameter contains the object to compare it to." TextColor="{StaticResource NormalLabelTextColor}" />
21+
<Label Text="Please enter 100 for true anyting else for false" TextColor="{StaticResource NormalLabelTextColor}" />
22+
<Entry
23+
x:Name="ExampleText"
24+
Placeholder="100 for True other for False"
25+
Text="{Binding Input}"
26+
TextColor="{StaticResource NormalLabelTextColor}" />
27+
<Label
28+
Padding="7,0,0,0"
29+
BindingContext="{x:Reference Name=ExampleText}"
30+
Text="{Binding Path=Text, Converter={StaticResource EqualConverter}, ConverterParameter=100}"
31+
TextColor="{StaticResource NormalLabelTextColor}" />
32+
</StackLayout>
33+
</ContentPage.Content>
34+
</pages:BasePage>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
using Xamarin.Forms;
5+
6+
namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
7+
{
8+
public partial class EqualConverterPage
9+
{
10+
public EqualConverterPage()
11+
{
12+
InitializeComponent();
13+
}
14+
}
15+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<pages:BasePage
3+
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Converters.IndexToArrayItemConverterPage"
4+
xmlns="http://xamarin.com/schemas/2014/forms"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:pages="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages"
7+
xmlns:vm="clr-namespace:Xamarin.CommunityToolkit.Sample.ViewModels.Converters"
8+
xmlns:xct="clr-namespace:Xamarin.CommunityToolkit.Converters;assembly=Xamarin.CommunityToolkit">
9+
<ContentPage.Resources>
10+
<ResourceDictionary>
11+
<xct:IndexToArrayItemConverter x:Key="IndexToArrayItemConverter" />
12+
<x:Array x:Key="ValuesArray" Type="{x:Type x:String}">
13+
<x:String>Value1</x:String>
14+
<x:String>Value2</x:String>
15+
<x:String>Value3</x:String>
16+
<x:String>Value4</x:String>
17+
<x:String>Value5</x:String>
18+
</x:Array>
19+
</ResourceDictionary>
20+
</ContentPage.Resources>
21+
<ContentPage.BindingContext>
22+
<vm:IndexToArrayItemConverterViewModel />
23+
</ContentPage.BindingContext>
24+
<ContentPage.Content>
25+
<StackLayout
26+
Padding="10,10"
27+
HorizontalOptions="Fill"
28+
Spacing="10"
29+
VerticalOptions="Fill">
30+
<Label Text="The IndexToArrayItemConverter is a converter that allows users to convert a int value binding to an item in an array. The int value being data bound represents the indexer used to access the array. The array is passed in through the ConverterParameter." TextColor="{StaticResource NormalLabelTextColor}" />
31+
<Entry
32+
Placeholder="Enter an index from 1 to 4 otherwise the app will crash"
33+
Text="{Binding Index}"
34+
TextColor="{StaticResource NormalLabelTextColor}" />
35+
<Label
36+
Padding="7,0,0,0"
37+
Text="{Binding Path=Index, Converter={StaticResource IndexToArrayItemConverter}, ConverterParameter={StaticResource ValuesArray}}"
38+
TextColor="{StaticResource NormalLabelTextColor}" />
39+
</StackLayout>
40+
</ContentPage.Content>
41+
</pages:BasePage>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
using Xamarin.Forms;
5+
6+
namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
7+
{
8+
public partial class IndexToArrayItemConverterPage
9+
{
10+
public IndexToArrayItemConverterPage()
11+
{
12+
InitializeComponent();
13+
}
14+
}
15+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<pages:BasePage
3+
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Converters.IntToBoolConverterPage"
4+
xmlns="http://xamarin.com/schemas/2014/forms"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:pages="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages"
7+
xmlns:vm="clr-namespace:Xamarin.CommunityToolkit.Sample.ViewModels.Converters"
8+
xmlns:xct="clr-namespace:Xamarin.CommunityToolkit.Converters;assembly=Xamarin.CommunityToolkit">
9+
<ContentPage.Resources>
10+
<ResourceDictionary>
11+
<xct:IntToBoolConverter x:Key="IntToBoolConverter" />
12+
</ResourceDictionary>
13+
</ContentPage.Resources>
14+
<ContentPage.BindingContext>
15+
<vm:IntToBoolConverterViewModel />
16+
</ContentPage.BindingContext>
17+
<ContentPage.Content>
18+
<StackLayout
19+
Padding="10,10"
20+
HorizontalOptions="Fill"
21+
Spacing="10"
22+
VerticalOptions="Fill">
23+
<Label Text="The IntToBoolConverter is a converter that allows users to convert an incoming int value to a bool. If the incoming int value is 0, it will be converted to false. Any other incoming value will be converted to true." TextColor="{StaticResource NormalLabelTextColor}" />
24+
<Label Text="Please enter 0 for false anyting else for True" TextColor="{StaticResource NormalLabelTextColor}" />
25+
<Entry
26+
x:Name="ExampleText"
27+
Placeholder="0 for false other for true"
28+
Text="{Binding Number}"
29+
TextColor="{StaticResource NormalLabelTextColor}" />
30+
<Label
31+
Padding="7,0,0,0"
32+
Text="{Binding Path=Number, Converter={StaticResource IntToBoolConverter}}"
33+
TextColor="{StaticResource NormalLabelTextColor}" />
34+
</StackLayout>
35+
</ContentPage.Content>
36+
</pages:BasePage>

0 commit comments

Comments
 (0)