Hi 80240195,
Welcome to our Microsoft Q&A platform!
If you want to transfer the data from page b to page a, usually we use delegate to refresh the "parent page".
Here is a simple demo using delegate to transfer data between pages.
MainPage.xaml
<Label x:Name="result"/>
<Button Text="SubPage" Clicked="Button_Clicked"/>
MainPage.xaml.cs
public partial class MainPage : ContentPage
{
// ...
private async void Button_Clicked(object sender, EventArgs e)
{
SubPage subpage = new SubPage();
//register event
subpage.TransfEvent += frm_TransfEvent;
await Navigation.PushAsync(subpage);
}
void frm_TransfEvent(string value)
{
result.Text = value;
}
}
SubPage.xaml
<Entry TextChanged="Entry_TextChanged"/>
SubPage.xaml.cs
public partial class SubPage : ContentPage
{
// ...
public event TransfDelegate TransfEvent;
private void Entry_TextChanged(object sender, TextChangedEventArgs e)
{
TransfEvent((sender as Entry).Text);
}
}
public delegate void TransfDelegate(String value);
Besides, we prefer to use MVVM pattern in Xamarin.Forms project. We can use data binding to transfer data.
The following is an MVVM example.
MainPage.xaml
<Label x:Name="result" Text="{Binding Result}"/>
<Button Text="SubPage" Command="{Binding ToSubPageCommand}"/>
MainPage.xaml.cs
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
this.BindingContext = new MainPageViewModel(this.Navigation);
}
}
SubPage.xaml
<Entry Text="{Binding Result}"/>
MainPageViewModel.cs
class MainPageViewModel : INotifyPropertyChanged
{
public ICommand ToSubPageCommand { get; private set; }
public INavigation Nav;
string result;
public string Result
{
get => result;
set
{
result = value;
OnPropertyChanged("Result");
}
}
public MainPageViewModel(INavigation nav)
{
ToSubPageCommand = new Command(ToSubPage);
Nav = nav;
}
SubPage subpage = new SubPage();
async void ToSubPage()
{
subpage.BindingContext = this;
await Nav.PushAsync(subpage);
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
Regards,
Kyle
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.