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

wixjitnu  于 2022-12-16  发布在  .NET
关注(0)|答案(2)|浏览(302)

我最近开始使用.Net MAUI。但是,我现在遇到了一个问题,我在互联网上找不到任何帮助。我希望当我单击一个按钮时,一个定义的单击函数被调用。但是,我不能将参数传递给“已单击”属性。我该怎么做?
我试图解决我的问题的帮助下,各种职位在不同的在线论坛,但这些职位都没有帮助,所以我正在创建自己的。
我的代码到目前为止:
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);
    }

感谢任何帮助:)

t1rydlwq

t1rydlwq1#

你可以从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);
    }

或者(与MVVM更加一致),您可以使用Command and CommandParameter

8wigbo56

8wigbo562#

正如Jason所说,您可以使用Command和CommandParameter。
这里有一个演示供您参考。

<TextCell Text="Customimze an Entry"
           Detail="Select text on focus"
           Command="{Binding NavigateCommand}"
           CommandParameter="{x:Type views:CustomizeEntryPage}" />

代码隐藏:

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

相关问题