Display a page with extended information

﹥>﹥吖頭↗ 提交于 2021-02-05 11:27:25

问题


I have the following functionality given below:

When clicking on details, i want the text that is displayed on the contentview shall be displayed on the new detailspage that is created by push async. How can I send parameters containing the info given in the content, e.g. title, category and description.

I have a tap gesture recognizer:

<Label.GestureRecognizers>
     <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
</Label.GestureRecognizers>

That then leads to creating a new page with the code behind:

private async void TapGestureRecognizer_Tapped(object sender, EventArgs e)
    {
        await Navigation.PushAsync(new DetailsPage(user), true);
    }

So how shall I bind the labels containing title, description and category to display in the new page that is created upon click?

Here's the xaml code behind the labels:

                        <StackLayout>
                            <StackLayout Orientation="Horizontal">
                                <BoxView Color="LightGray"
                                     WidthRequest="90"
                                     HeightRequest="90"
                                     HorizontalOptions="Center"
                                     VerticalOptions="Center"/>
                                <StackLayout Padding="0">

                                    <Label x:Name="Title" Text="Title"
                                       FontSize="22"
                                       FontFamily="Grotesk"
                                       TextColor="Black"
                                       Margin="10, -2, 0, 0"
                                       VerticalOptions="Fill" />
                                    <Label x:Name="Category" Text="Category"
                                       FontSize="16"
                                       FontFamily="Grotesk"
                                       TextColor="Gray"
                                       Margin="10, -2, 0, 0"
                                       VerticalOptions="Fill" />
                                    <Label x:Name="Desc" Text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. etc.etc"
                                       Margin="10, -4, 0, 0"
                                       FontSize="15"
                                        FontFamily="Latow"
                                       VerticalOptions="Start"
                                       MaxLines="3"/>
                                </StackLayout>
                            </StackLayout>

回答1:


use the Label's BindingContext

private async void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{

    var label = (Label)sender;

    // you will need to cast it to the correct type
    var item = (MyClass)label.BindingContext;

    // then pass it to your page
    await Navigation.PushAsync(new DetailsPage(user,item), true);
}


来源:https://stackoverflow.com/questions/64970522/display-a-page-with-extended-information

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!