简体   繁体   中英

How to make change in APP.xaml.cs code to pass a parameter from one page to another in Xamarin?

I want to pass a parameter from one page to another. I used this code for one of the pages:

public TabbedMainPage(string parameter)
{
    InitializeComponent();
    On<Android>().SetToolbarPlacement(ToolbarPlacement.Bottom);
    On<Android>().SetIsSmoothScrollEnabled(false);
    lbl01.Text = parameter;
}

App.xaml.cs:

public App()
{
    InitializeComponent();

    MainPage = new TabbedMainPage();
}

The problem is that App.xaml.cs has error under TabbedMainPage . I tried MainPage = new TabbedMainPage(parameter); but the problem still exists. Please help me.

You could take reference for the code below.

TabbedPage1.xaml: I set the tab in bottom in 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>

Code behind:

 public TabbedPage1(string parameter)
    {
        InitializeComponent();
        lbl01.Text = parameter;
    }

App.xaml.cs:

public App()
    {
        InitializeComponent();

        MainPage = new NavigationPage(new TabbedPage1("Hello TabbedPage"));

    }

在此处输入图片说明

For mor information about passing data, you could refer to the MS docs. https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/navigation/hierarchical

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM