0
votes

I hope someone could help me understand how it could works.. I have a project in Xamarin Forms

I have a page1 with code below

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

    }
}

and in the second page 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>

So when I click on the button, I want to go to Page2 and pass datas from the previous page or another Modelpage.

What is the good way when you have lists of datas and want to use them in several pages ? Do you recommend use a db ?

My problem is similar as a ListView ( when you click on item , you can go to another detailpage) but here I use a bindable layout.

1
you typically pass data to pages using their constructors, just like you do with any C# classJason
Yes thanks I just didn't how to do it.DREAMZR

1 Answers

1
votes

As Jason's reply, you can pass data to another pages using constructors.

For the first page, you can find the current select item in Button_click event.

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

Then modify the second page constructors that having one parameter.

 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>

Please note: you need to bind ListPrograms1 to StackLayout BindableLayout.ItemsSource, not List1, because I don't find where is List1.

enter image description here