Pass command to custom component. #688
-
I'm not sure if this is a bug or if I'm doing something wrong, but I cannot pass Example: // custom.header.xaml <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
xmlns:font="clr-namespace:mauitest.Model.Font"
x:Class="mauitest.View.Header"
x:Name="HeaderComponent"
BindingContext="{x:Reference HeaderComponent}">
<Grid
ColumnDefinitions="Auto,*,Auto"
ColumnSpacing="20"
Margin="10,0,10,0">
<toolkit:AvatarView
Grid.Column="0"
BackgroundColor="Transparent"
BorderColor="Transparent"
FontSize="22"
FontFamily="FaSolid"
Text="{x:Static font:FaSolidIcons.ArrowLeft}"
HorizontalOptions="Start">
<toolkit:AvatarView.GestureRecognizers>
<TapGestureRecognizer Command="{Binding BackButtonCommand}"/>
</toolkit:AvatarView.GestureRecognizers>
</toolkit:AvatarView>
<Label
Grid.Column="1"
Text="{Binding Text}"
HorizontalOptions="Center"
VerticalOptions="Center"
Style="{StaticResource LargeLabel}"/>
<toolkit:AvatarView
Grid.Column="2"
BackgroundColor="Transparent"
BorderColor="Transparent"
FontSize="18"
FontFamily="FaSolid"
Text="{x:Static font:FaSolidIcons.House}"
HorizontalOptions="Start">
<toolkit:AvatarView.GestureRecognizers>
<TapGestureRecognizer Command="{Binding HomeButtonCommand}"/>
</toolkit:AvatarView.GestureRecognizers>
</toolkit:AvatarView>
</Grid>
</ContentPage>
// custom.header.xaml.cs using CommunityToolkit.Maui.Views;
namespace mauitest.View;
public partial class Header : ContentPage
{
public static readonly BindableProperty TextProperty =
BindableProperty.Create(nameof(Text), typeof(string), typeof(Header));
public string Text
{
get => (string)GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
public static readonly BindableProperty BackButtonCommandProperty =
BindableProperty.Create(nameof(BackButtonCommand), typeof(RelayCommand), typeof(Header));
public RelayCommand BackButtonCommand
{
get => (RelayCommand)GetValue(BackButtonCommandProperty);
set => SetValue(BackButtonCommandProperty, value);
}
public static readonly BindableProperty HomeButtonCommandProperty =
BindableProperty.Create(nameof(HomeButtonCommand), typeof(RelayCommand), typeof(Header));
public RelayCommand HomeButtonCommand
{
get => (RelayCommand)GetValue(HomeButtonCommandProperty);
set => SetValue(HomeButtonCommandProperty, value);
}
public Header()
{
InitializeComponent();
}
} // main.component <view:Header
Text="Random text"
BackButtonCommand="{Binding GoToBackCommand}"
HomeButtonCommand="{Binding GoToMainCommand}"/> When I launch the application, I see "Random text", but when I click on the buttons (avatarview), nothing happens. |
Beta Was this translation helpful? Give feedback.
Answered by
nosferatu500
Oct 12, 2022
Replies: 2 comments
-
My bad. I have to use |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
nosferatu500
-
Closed as answered |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My bad. I have to use
AsyncRelayCommand