c# - Xamarin 将参数从后面的代码传递到不同的 xaml
问题描述
在我的应用程序中,Page1 中有一个搜索栏,我希望它做两件主要的事情:
- 将用户定向到 Page2
- 保存搜索到的文本并将其用作 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 到它自己的视图模型。
如果需要更多代码或其他信息,我会添加它,非常感谢任何帮助!
解决方案
推荐阅读
- angular - Angular2:用于路由的共享组件
- sql - 选择符合 PostgreSQL 条件的最新连续记录
- c++ - 创建向量 LLVM 的问题
- azure-active-directory - 从 Azure AD 获取 SAML 属性中的电子邮件和经理 ID
- python - 无法在python中安装任何包
- oracle - Oracle 12c 程序
- php - 如何在 Laravel 中使用 sendmail?
- jquery - 使用 jQuery 在图像类中附加 src
- npm - “尝试安装 webpack 时找不到模块‘webpack/schemas/WebpackOptions.json’”
- python - SQLite 和 Python - 从数据库中获取值