c# - 如何更改 APP.xaml.cs 代码以在 Xamarin 中将参数从一个页面传递到另一个页面?
问题描述
我想将参数从一个页面传递到另一个页面。我将此代码用于其中一个页面:
public TabbedMainPage(string parameter)
{
InitializeComponent();
On<Android>().SetToolbarPlacement(ToolbarPlacement.Bottom);
On<Android>().SetIsSmoothScrollEnabled(false);
lbl01.Text = parameter;
}
应用程序.xaml.cs:
public App()
{
InitializeComponent();
MainPage = new TabbedMainPage();
}
问题是 App.xaml.cs 在TabbedMainPage
. 我试过MainPage = new TabbedMainPage(parameter);
了,但问题仍然存在。请帮我。
解决方案
您可以参考下面的代码。
TabbedPage1.xaml:我在 xaml 中将选项卡设置在底部。
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App1.TabbedPage1"
xmlns:tabs="clr-namespace:App1"
xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
CurrentPageChanged="TabbedPage_CurrentPageChanged"
android:TabbedPage.ToolbarPlacement="Bottom"
>
<!--Pages can be added as references or inline-->
<tabs:Page1 Title="Tab1">
<Label x:Name="lbl01"></Label>
</tabs:Page1>
<tabs:Page2 Title="Tab2"/>
<tabs:Page3 Title="Tab3"/>
<tabs:Page4 Title="Tab4"/>
<tabs:Page5 Title="Tab5"/>
<tabs:Page6 Title="Tab6"/>
</TabbedPage>
后面的代码:
public TabbedPage1(string parameter)
{
InitializeComponent();
lbl01.Text = parameter;
}
应用程序.xaml.cs:
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new TabbedPage1("Hello TabbedPage"));
}
有关传递数据的更多信息,您可以参考 MS 文档。 https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/navigation/hierarchical
推荐阅读
- c# - 如何将作用域区域(管理器)暴露给 ViewModel
- sass - 如何处理 BrowserSync 和 gulp-watch?
- linux - 使用“&”在bash中并行处理并等待
- perl - 如何在 Perl 脚本中发送邮件?
- python-3.x - 如何将元素插入嵌套字典
- hadoop - 如何将配置单元查询输出重定向到带有空格的标题和列名的文本文件
- kubernetes - Nginx Ingress 控制器设置问题
- python - Mixin 方法 super() 调用在 PyCharm 中生成一个未解析的属性引用
- sql - 警告:使用编译错误创建的类型
- visual-studio-code - 编辑器和小地图中文件中的灰色突出显示行?