Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1134 Update samples #1219

Merged
merged 9 commits into from
Jun 13, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
xmlns:pages="clr-namespace:CommunityToolkit.Maui.Sample.Pages"
xmlns:mct="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
xmlns:vm="clr-namespace:CommunityToolkit.Maui.Sample.ViewModels.Behaviors"
xmlns:system="clr-namespace:System;assembly=mscorlib"
x:Class="CommunityToolkit.Maui.Sample.Pages.Behaviors.EventToCommandBehaviorPage"
x:TypeArguments="vm:EventToCommandBehaviorViewModel"
x:DataType="vm:EventToCommandBehaviorViewModel">
Expand All @@ -16,7 +17,7 @@

<Label Text="{Binding ClickCount, StringFormat='{0} clicks'}" />

<Button Text="Click Me"
<Button Text="Increment (No Args)"
TextColor="White"
BackgroundColor="{StaticResource NormalButtonBackgroundColor}">
<Button.Behaviors>
Expand All @@ -25,5 +26,17 @@
Command="{Binding IncrementCommand}" />
</Button.Behaviors>
</Button>


<Button Text="Click Me (Pass event args)"
TextColor="White"
BackgroundColor="{StaticResource NormalButtonBackgroundColor}">
<Button.Behaviors>
<mct:EventToCommandBehavior
x:TypeArguments="system:EventArgs"
EventName="Clicked"
Command="{Binding IncrementWithArgsCommand}" />
</Button.Behaviors>
</Button>
</VerticalStackLayout>
</pages:BasePage>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace CommunityToolkit.Maui.Sample.ViewModels.Behaviors;

Expand All @@ -8,10 +9,15 @@ public partial class EventToCommandBehaviorViewModel : BaseViewModel
[ObservableProperty]
int clickCount;

public EventToCommandBehaviorViewModel()
[RelayCommand]
void Increment()
{
IncrementCommand = new Command(() => ClickCount++);
ClickCount++;
}

public ICommand IncrementCommand { get; }
[RelayCommand]
void IncrementWithArgs(EventArgs eventArgs)
{
ClickCount++;
}
}