首页 > 解决方案 > 如何更改 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);了,但问题仍然存在。请帮我。

标签: c#xamarin

解决方案


您可以参考下面的代码。

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


推荐阅读