首页 > 解决方案 > Xamarin 将参数从后面的代码传递到不同的 xaml

问题描述

在我的应用程序中,Page1 中有一个搜索栏,我希望它做两件主要的事情:

  1. 将用户定向到 Page2
  2. 保存搜索到的文本并将其用作 http 搜索

我设法做到了这两点,现在我想在 Page2 中使用来自服务器的信息。

我没有成功地在 MVVM 中实现这一点,而且只在后面的代码上实现。

Page1 后面的代码:

namespace MyApp.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Page1: ContentPage
    {
        HttpClient client;
        public Page1()
        {
            InitializeComponent();
            client = new HttpClient();
        }

        private async void SearchBar_SearchButtonPressed(object sender, EventArgs e)
        {
            await Shell.Current.GoToAsync(nameof(Page2));

            Uri uri = new Uri(string.Format("url" + SearchBar.Text, string.Empty));

            HttpResponseMessage response = await client.GetAsync(uri);
           
            if (response.IsSuccessStatusCode)
            {
                string content = await response.Content.ReadAsStringAsync(); //var needed in Page2
            }
        }        
    }
}

if 中的“content”变量是我想在 Page2.xaml 中使用的变量,它有 BindingContext 到它自己的视图模型。

如果需要更多代码或其他信息,我会添加它,非常感谢任何帮助!

标签: c#xamlxamarinxamarin.forms

解决方案


推荐阅读