首页 > 解决方案 > 如何从上一页的文本框中获取变量到下一页?

问题描述

我正在尝试创建一个搜索框,引导到另一个页面来执行搜索。例如在 Inquirypage 中搜索 --> 在 Listpage 中获取结果。我如何从查询页面获取值到列表页面,因为它们不在同一个类中。我可以得到一些见解吗?谢谢

从第一页搜索按钮:

public async void FromBPSearchTap(object sender, EventArgs e)
    {
        var BPSearch = Customer.Text;
        if (string.IsNullOrWhiteSpace(BPSearch))
        {
            BPSearch = string.Empty;
        }
        BPSearch = BPSearch.ToUpperInvariant();
    }

标签: c#xamarin.forms

解决方案


将字符串作为参数传递给下一页的构造函数。
示例代码如下:

在你的InquiryPage.xaml.cs

    private async void FromBPSearchTap(object sender, System.EventArgs e)
    {
        //...
        //your code with BPSearch     
        await Navigation.PushAsync(new ListPage(BPSearch));
    }

在你的下一个ListPage.xaml.cs

    string _searchText
    public ListPage(string searchText)
    {
        InitializeComponent();
        _searchText = searchText;  //have your text in ListPage now
    }

推荐阅读