question

80240195 avatar image
0 Votes"
80240195 asked 80240195 commented

how to get data from other page?

I have page a and page b.
In page a, open page b.
in page b, user input some data, after close page b,
How to let a get the data in b.
I code below, but not get.
in page a:
LiveStreamPage liveStreamPage = new LiveStreamPage();
await Navigation.PushAsync(liveStreamPage);
if (liveStreamPage.url != "")
{
((MainViewModel)DetailPage.BindingContext).OnAppearing(liveStreamPage.url);
}
this.IsPresented = false;
in page b, the b is LiveStreamPage
public partial class LiveStreamPage : ContentPage
{
public String url = "";
public LiveStreamPage()
{
InitializeComponent();
}

     private async void Button_Clicked(object sender, EventArgs e)
     {
         url = editor.Text.Trim();
         await Navigation.PopAsync();
     }
 }
dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

KyleWang-MSFT avatar image
1 Vote"
KyleWang-MSFT answered 80240195 commented

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.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@80240195 Does this answer help you? If not, you can post the problem here.

0 Votes 0 ·

Thank you.
I will try.

0 Votes 0 ·