简体   繁体   English

在 xamarin.forms 中将标签从一页添加到另一页的列表视图

[英]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".我正在 xamarin.forms 中创建一个应用程序,我想在“page2”上创建一个新的 label,其中包含来自“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.我认为问题在于我在页面之间导航的方式,因为我知道添加 label 代码有效,因为我尝试将它放在一个页面上并且有效。 but when i try adding the label and then navigating to "page2" nothing is there.但是当我尝试添加 label 然后导航到“page2”时,什么也没有。 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.现在我有一个带有两个按钮的“MainPage”,一个用于“page1”,一个用于“page2”我还尝试将“page2”的导航按钮放在用户输入的页面“page1”上。 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非常感谢: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然后在Page1的构造函数中

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

Adding labels to a listview from one page to another in xamarin.forms在 xamarin.forms 中将标签从一页添加到另一页的列表视图

Yes, we usually pass the data by the constructor of our ContentPage just as Jason said.是的,我们通常像 Jason 所说的那样通过我们的 ContentPage 的构造函数传递数据。

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.如果我们想在从 page1 返回到 page2 时保存传递的数据,我们可以为 page2 定义一个全局数据。

You can refer to the following code:您可以参考以下代码:

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

}

Page1.xaml.cs页面1.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页面2.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第2.xaml页

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Xamarin.Forms中将字符串从一页传递到另一页 - Passing a string from one page to another in Xamarin.Forms Xamarin.Forms:在ListView中添加标签? - Xamarin.Forms: add labels inside ListView? 如何在Xamarin.Forms中将参数从一个页面传递到另一个页面? - How to pass a parameter from one Page to another Page in Xamarin.Forms? 如何从Xamarin.forms中的按钮单击从一个页面移动到另一个页面 - how to move from one page to another from button click in Xamarin.forms Xamarin.Forms - 从另一个文件将约会添加到同步融合时间表 - Xamarin.Forms - Adding appointment to syncfusion schedule from another file Xamarin.forms ListView单击到下一页 - Xamarin.forms ListView click to next page Xamarin.Forms Prism-使用导航服务从一个详细信息页面导航到另一个 - Xamarin.Forms Prism - Using navigation service to navigate from one detail page to another 在使用 Xamarin.Essentials 抖动检测事件时,未在 Xamarin.Forms 中从一页导航到另一页 - Not navigating from one page to another page in Xamarin.Forms while using using Xamarin.Essentials shake detect event hybridWebView 页面从 Xamarin.Forms 中的 Javascript 打开另一个 ContentPage - hybridWebView page open another ContentPage from Javascript in Xamarin.Forms Xamarin.Forms中如何获取ListView的ViewCell数据并发送到另一个页面 - How to get ListView's ViewCell data and send it to another page in Xamarin.Forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM