简体   繁体   English

如何在 .NET MAUI 的按钮上使用“点击”方法传递参数?

[英]How can I pass parameters using the "Clicked"-Method on a button in .NET MAUI?

I have recently started using.Net MAUI.我最近开始使用.Net MAUI。 however, I have now encountered a problem with which I could not find any help on the inte.net.然而,我现在遇到了一个问题,我无法在 inte.net 上找到任何帮助。 I want when I click a button that a defined click function is called.当我单击一个按钮时,我希望调用已定义的单击 function。 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: 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#: C#:

private async void onStudentSelected(object sender, EventArgs e, int id)
    {
        await Shell.Current.GoToAsync("StudentDetail" + id);
    }

Am grateful for any help:)感谢您的帮助:)

you can get the id from the BindingContext您可以从BindingContext获取id

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或者(更符合 MVVM)你可以使用Command 和 CommandParameter

Just as Jason said, you can use the Command and CommandParameter .正如 Jason 所说,您可以使用Command 和 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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM