Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
788 views
in Technique[技术] by (71.8m points)

xamarin.forms - 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 ();
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...