bhr
bhr

Reputation: 555

Xamarin pass data to another page

I want to pass the entered name to page 1. I handled the variable, which means that when you enter your name and click the button it will be put in variable valueName. But I cannot pass this variable to page 1. Can someone help me with this?

<MasterDetailPage.Master>
    <ContentPage Padding="10" BackgroundColor="Beige" Title="Master" Icon="">
        <ContentPage.Content>
            <StackLayout Margin="5,30,5,5">
                <Label Text="Master Page">
                </Label>
                <Label Text="Name:" >
                </Label>
                <Entry x:Name="name" Placeholder="Enter your name"></Entry>
                <Button Text="Click this button to show name on Page 1" 
                 BackgroundColor="Yellow" Clicked="Button_Clicked"></Button>
            </StackLayout>
        </ContentPage.Content>
    </ContentPage>
</MasterDetailPage.Master>

public partial class MainPage : MasterDetailPage
{
    public MainPage()
    {
        InitializeComponent();
        Detail = new NavigationPage(new Page1());
        IsPresented = true;
    }
    private void Button_Clicked(object sender, EventArgs e)
    {
        Detail = new NavigationPage(new Page1());
        IsPresented = false;
        string valueName = name.Text;
    }
}

<ContentPage.Content>
    <StackLayout>
        <Label 
            Text="Page 1">
        </Label>

        <Label 
            Text="Text of name must be here">
        </Label>

    </StackLayout>
</ContentPage.Content>

public partial class Page1 : ContentPage
{
    public Page1 ()
    {
        InitializeComponent ();
    }
}

Upvotes: 1

Views: 8578

Answers (1)

Jason
Jason

Reputation: 89204

Pages are just classes. You can pass parameters to them using the constructor, public properties, public methods, etc

For example, to pass via the constructor

private void Button_Clicked(object sender, EventArgs e)
{
    string valueName = name.Text;
    Detail = new NavigationPage(new Page1(valuename));
    IsPresented = false;       
}

in Page1, modify the constructor to accept a parameter

string _name;

public Page1 (string name)
{
    InitializeComponent ();

    _name = name;
}

Upvotes: 7

Related Questions