Hello,
Welcome to our Microsoft Q&A platform!
You could use Action
to pass value. There are two pages in my demo, when I click the button and pop the second page to the previous page(first page), I pass the label text of the second page. You can have a try with the following code:
PageOne.xaml
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="PassValueDemo.PageOne"
Title="PageOne">
<ContentPage.Content>
<StackLayout>
<Label x:Name="showResultLabel" Text="none" FontSize="Large" BackgroundColor="Pink"></Label>
<Button Text="Push button"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
Clicked="Button_Clicked"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
PageOne.xaml.cs
private void Button_Clicked(object sender, EventArgs e)
{
PageTwo second = new PageTwo();
second.PassValueAction = (passvalue) => {
Console.WriteLine("receive: {0}", passvalue);
showResultLabel.Text = passvalue;
};
Navigation.PushAsync(second);
}
PageTwo.xaml
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="PassValueDemo.PageTwo"
Title="PageTwo">
<ContentPage.Content>
<StackLayout BackgroundColor="LightBlue">
<Label x:Name="resultLabel" Text="result"></Label>
<Button Text="Pop button"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
Clicked="Button_Clicked"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
PageTwo.xaml.cs
public partial class PageTwo : ContentPage
{
public Action<String> PassValueAction;//use action to pass value
static int tag;
public PageTwo()
{
InitializeComponent();
}
private void Button_Clicked(object sender, EventArgs e)
{
tag++;//test
PassValueAction(resultLabel.Text + tag.ToString());
Navigation.PopAsync();
}
}
Best Regards,
Wenyan Zhang
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.