Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
818 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...