【问题标题】:How to navigate between page with datas如何在包含数据的页面之间导航
【发布时间】:2020-06-04 09:13:23
【问题描述】:

我希望有人可以帮助我了解它是如何工作的.. 我在 Xamarin Forms 中有一个项目

我有一个page1,下面有代码

Page1.xaml

<StackLayout BindableLayout.ItemsSource="{Binding List1}">
  <BindableLayout.ItemTemplate>
         <DataTemplate>
             <AbsoluteLayout>
                 <Button Text="{Binding NameP} Clicked="Button_Clicked"/>
             </AbsoluteLayout>
         </DataTemplate>
   </BindableLayout.ItemTemplate>
 </StackLayout>

Page1.cs

using System.Collections.Generic;
using Xamarin.Forms;

namespace app.Views
{
    public partial class Page1 : ContentPage
    {
        public Page1()
        {
            InitializeComponent();

            ListPrograms1 = new List<Programmes1>();

            {
                ListPrograms1.Add(new Programmes1() { NameP = "Tomato", Detail = "xxxxx" });
                ListPrograms1.Add(new Programmes1() { NameP = "Pepperoni", Detail = "yyyyy" });

            }
            BindingContext = this;
        }

        public List<Programmes1> ListPrograms1
        {
            get; set;
        }


        public class Programmes1
        {
            public string NameP { get; set; }
           public string Detail { get; set; }
        }

        public async void Button_Clicked(object sender, System.EventArgs e)
        {
            await Navigation.PushAsync(new Page2());
        }

    }
}

在第二页 Page2.xaml

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="app.Views.Page2">

<StackLayout VerticalOptions="Start" HorizontalOptions="Center">
                <Label Text={Binding NameP}" />
              <Label Text={Binding Detail}" />
            </StackLayout>
</ContentPage>

所以当我点击按钮时,我想去Page2并从前一页或另一个Modelpage传递数据。

当您有数据列表并想在多个页面中使用它们时,有什么好方法? 你推荐使用数据库吗?

我的问题与 ListView 类似(当您单击 item 时,您可以转到另一个详细信息页面)但这里我使用的是可绑定布局。

【问题讨论】:

  • 您通常使用页面的构造函数将数据传递给页面,就像使用任何 C# 类一样
  • 是的,谢谢,我只是不知道该怎么做。

标签: c# xamarin xamarin.forms


【解决方案1】:

作为 Jason 的回复,您可以使用构造函数将数据传递到其他页面。

对于第一页,可以在Button_click事件中找到当前的选择项。

   private async void Button_Clicked(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        Programmes1 data = ListPrograms1.Find(s => s.NameP == btn.Text);
        await Navigation.PushAsync(new Page26(data));
    }

然后修改具有一个参数的第二页构造函数。

 public partial class Page26 : ContentPage
{
    public Programmes1 Model { get; set; }

    public Page26(Programmes1 model)
    {
        InitializeComponent();
        Model = model;
        Console.WriteLine(Model.NameP);
        this.BindingContext = this;
    }

}

<StackLayout HorizontalOptions="Center" VerticalOptions="Start">
            <Label Text="{Binding Model.NameP}" />
            <Label Text="{Binding Model.Detail}" />


        </StackLayout>

请注意:您需要将 ListPrograms1 绑定到 StackLayout BindableLayout.ItemsSource,而不是 List1,因为我没有找到 List1 在哪里。

【讨论】:

  • 非常感谢。你带走了我的一部分神秘感:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-10
  • 1970-01-01
  • 1970-01-01
  • 2013-12-13
  • 2011-05-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多