Reputation: 127
I have recently started using .Net MAUI. however, I have now encountered a problem with which I could not find any help on the internet. I want when I click a button that a defined click function is called. However, I can't pass a parameter to the "Clicked" attribute. How do I do that?
I tried to solve my problem with the help of various posts in different online forums, but none of these posts helped and so I am creating my own.
My code so far:
XAML:
<Grid RowSpacing="50" Margin="50">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Border Stroke="Transparent"
StrokeThickness="3"
StrokeShape="RoundRectangle 30,30,30,30"
HorizontalOptions="Center"
BackgroundColor="White"
Grid.Row="0"
Grid.Column="0">
<Border.GestureRecognizers>
<TapGestureRecognizer Tapped="onStudentSelected"/> <!-- Here i want to give a param -->
</Border.GestureRecognizers>
<VerticalStackLayout WidthRequest="300" HeightRequest="250">
<Border Stroke="#21B1FF"
StrokeThickness="3"
StrokeShape="RoundRectangle 15,15, 15, 15"
HorizontalOptions="Center"
BackgroundColor="White"
Margin="10">
<VerticalStackLayout WidthRequest="240">
<Label FontSize="25" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" Text="Max Muster" Padding="10"/>
</VerticalStackLayout>
</Border>
<Border Stroke="#21B1FF"
StrokeThickness="3"
StrokeShape="RoundRectangle 15,15, 15, 15"
HorizontalOptions="Center"
BackgroundColor="White">
<VerticalStackLayout WidthRequest="240">
<Label Margin="5" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" Text="Newest Grade: 5.8" Padding="10"/>
<Label Margin="5" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" Text="Average: 4.5" Padding="10"/>
<Label Margin="5" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" Text="Best Subject: Math" Padding="10"/>
</VerticalStackLayout>
</Border>
</VerticalStackLayout>
</Border>
</Grid>
C#:
private async void onStudentSelected(object sender, EventArgs e, int id)
{
await Shell.Current.GoToAsync("StudentDetail" + id);
}
Am grateful for any help :)
Upvotes: 5
Views: 9916
Reputation: 854
So I am not sure why the eventArgs are empyt but this works for me pulling the sender from the Button Click Event. Note: Specify the CommandParameter Binding. Otherwise it wont work.
XAML
<Button x:Name="btnsave" Margin="1" CommandParameter="{Binding .}" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="1" Clicked="Btnsave_Clicked" Text="Save" />
C# Code
private void Btnsave_Clicked(object sender, EventArgs e)
{
var ShipmentParams = ((Button)sender).CommandParameter;
var ShipmentDetails = (ShipmentDetailsModel)ShipmentParams;
}
Upvotes: 2
Reputation: 55
You can use CommandParameter with the tapped event
<Border.GestureRecognizers>
<TapGestureRecognizer CommandParameter="{Binding .}" Tapped="onStudentSelected"/>
</Border.GestureRecognizers>
Code Behind:
private async void onStudentSelected(object sender, EventArgs e)
{
var student = (e.Parameter) as Student;
await Shell.Current.GoToAsync("StudentDetail" + student.id);
}
Upvotes: 0
Reputation: 4586
Just as Jason said, you can use the Command and CommandParameter.
Here is the demo for you to refer to.
<TextCell Text="Customimze an Entry"
Detail="Select text on focus"
Command="{Binding NavigateCommand}"
CommandParameter="{x:Type views:CustomizeEntryPage}" />
Code behind:
public ICommand NavigateCommand { get; private set; }
public MainPage()
{
InitializeComponent();
NavigateCommand = new Command<Type>(
async (Type pageType) =>
{
Page page = (Page)Activator.CreateInstance(pageType);
await Navigation.PushAsync(page);
});
BindingContext = this;
}
Upvotes: 0
Reputation: 89169
you can get the id
from the BindingContext
private async void onStudentSelected(object sender, EventArgs e)
{
// assuming your Model class is "Student"
var border = (Border)sender;
var item = (Student)border.BindingContext;
var id = item.Id;
await Shell.Current.GoToAsync("StudentDetail" + id);
}
alternately (and more aligned with MVVM) you can use Command and CommandParameter
Upvotes: 10