简体   繁体   中英

Adding labels to a listview from one page to another in xamarin.forms

Hi I am still very much a beginner programmer, so this might be a trivial question. I am creating an app in xamarin.forms, where i want to create a new label on "page2" with the text from a userinput on "page1". I think the problem lies in the way i navigate between pages, because i know the adding label code works, cuz i tried putting it on a single page and that worked. but when i try adding the label and then navigating to "page2" nothing is there. Right now I have a "MainPage" with two buttons, one for "page1" and one for "page2" I have also tried putting the Navigation button for "page2" on "page1", the page where the user input is. I am using this navigation:

public MainPage()
    {
        InitializeComponent();
    }
    private void BestilClicked(object sender, EventArgs e)
    {
        Navigation.PushAsync(new Page1());
    }
    private void HentClicked(object sender, EventArgs e)
    {
        Navigation.PushAsync(new Page2());
    }

If there is any more code that you find would be relevant feel free to say it, and i will gladly provide it. Thanks alot:D

you need to pass the data to the next page, something like this

Navigation.PushAsync(new Page1(SomeUserEntry.Text));

then in the constructor for Page1

public Page1(string input)
{
   // do something with input here
}

Adding labels to a listview from one page to another in xamarin.forms

Yes, we usually pass the data by the constructor of our ContentPage just as Jason said.

And if we want to keep the passed data saved if we come back from page1 to page2,we can define a global data for page2.

You can refer to the following code:

public class MyData
{
    public static List<string> dataList = new List<string>();

}

Page1.xaml.cs

public partial class Page1 : ContentPage
{
    public Page1()
    {
        InitializeComponent();
    }

    private async void Button_Clicked(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(mEntry.Text))
        {
            // method 1: pass data by constructor
            await Navigation.PushAsync(new Page2(mEntry.Text));
        }
        else
        {
            await DisplayAlert("Alert", "The input is empty!", "OK");
        }

    }
}

Page2.xaml.cs

   public partial class Page2 : ContentPage
{
    public Page2(string input)
    {
        InitializeComponent();

        // here we can get the passed data from page1 and we can add the 
        //data  to MyData.dataList

        MyData.dataList.Add(input);
      
        // set the ItemsSource  for the mListView in page2
        mListView.ItemsSource = MyData.dataList;
    }
}

Page2.xaml

   <ContentPage.Content>
    <StackLayout  x:Name="mStackLayout">
        <Label Text="Welcome to Page2"
            VerticalOptions="Start" 
            HorizontalOptions="CenterAndExpand" />

        <ListView x:Name="mListView"  >
            
            
        </ListView>
    </StackLayout>
</ContentPage.Content>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM