我想将输入的名称传递给第1页。 我处理了变量,这意味着当您输入名称并单击按钮时,它将被放入变量valueName中。 但是我不能将此变量传递给第1页。 有人可以帮我吗?
<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 ();
}
}
答案 0 :(得分:1)
页面只是类。您可以使用构造函数,公共属性,公共方法等向其传递参数
例如,通过构造函数传递
private void Button_Clicked(object sender, EventArgs e)
{
string valueName = name.Text;
Detail = new NavigationPage(new Page1(valuename));
IsPresented = false;
}
在第1页中,修改构造函数以接受参数
string _name;
public Page1 (string name)
{
InitializeComponent ();
_name = name;
}