【问题标题】:Display a page with extended information显示包含扩展信息的页面
【发布时间】:2021-03-07 17:40:36
【问题描述】:

我有以下功能:

单击详细信息时,我希望内容视图上显示的文本应显示在由推送异步创建的新详细信息页面上。如何发送包含内容中给出的信息的参数,例如标题、类别和描述。

我有一个点击手势识别器:

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

这会导致创建一个包含代码的新页面:

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

那么我应该如何绑定包含标题,描述和类别的标签以显示在点击时创建的新页面中?

这是标签后面的 xaml 代码:

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

【问题讨论】:

    标签: c# xaml xamarin xamarin.forms xamarin.android


    【解决方案1】:

    使用LabelBindingContext

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

    【讨论】:

    • 谢谢!然后如何从我的详细信息页面中的项目中提取标题、类别和描述,以便在那里显示它们?我在问题的内容后面添加了 xaml 代码
    • item 对象应该包含您需要的所有数据,您只需在新页面中设置 BindingContext 并在 XAML 中使用数据绑定
    • 在这种情况下如何设置 bindingContext?我似乎无法让它工作
    • this.BindingContext = TheParameterPassedOnTheConstructor;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-12
    相关资源
    最近更新 更多