how to get data from other page?

钢 屈 371 Reputation points
2021-08-01T10:27:33.847+00:00

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();
    }
}
Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Answer accepted by question author
  1. Kyle Wang 5,531 Reputation points Microsoft External Staff
    2021-08-02T02:30:06.173+00:00

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.