Home > Software design >  How can I pass parameters using the "Clicked"-Method on a button in .NET MAUI?
How can I pass parameters using the "Clicked"-Method on a button in .NET MAUI?

Time:12-12

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 :)

CodePudding user response:

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

CodePudding user response:

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;
    }
  • Related