Hi i am just wondering if there is a way to pop data back in xamarin forms

MysticsDen 70 Reputation points
2023-02-17T21:27:28.1233333+00:00

my current code works but only so that it creates a new page instead of popping the data into the previous one

// this is where the data is getting pulled from
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace MysticsGuardian.Views
{
	[XamlCompilation(XamlCompilationOptions.Compile)]
	public partial class EmotesPage : ContentPage, INotifyPropertyChanged
	{
        ObservableCollection<EmotesModel> emotes;
        public EmotesPage()
        {
            InitializeComponent();
            emotes = new ObservableCollection<EmotesModel>
            {
                new EmotesModel {Name ="No Icon", Image="Achievement_Frame.png", Category= EmotesModel.Group.Emote},
                new EmotesModel {Name ="Guardian", Image="Police_Icon.png", Category= EmotesModel.Group.Emote},
                new EmotesModel {Name ="Guardian", Image="Danger_Icon.png", Category= EmotesModel.Group.Emote},
                new EmotesModel {Name ="Guardian", Image="Danger_Icon.png", Category= EmotesModel.Group.Emote},
                new EmotesModel {Name ="Guardian", Image="Danger_Icon.png", Category= EmotesModel.Group.Emote},
        };
			EmotesView.ItemsSource = emotes;
        }
        

        
        async void EmotesView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var EmoteSelected = e.CurrentSelection[0] as EmotesModel;
            var EmoteClicked = new Profile(EmoteSelected);
            await Navigation.PushModalAsync(EmoteClicked);
            
            
        }
// this is where the data goes and i would like the data to pop rather than push!

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.CommunityToolkit.UI.Views;
using Xamarin.Essentials;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Xamarin.CommunityToolkit.Extensions;


namespace MysticsGuardian.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Profile : TabbedPage 
    {
        public Profile()
        {
            InitializeComponent();
            Emote1.Source = "Achievement_Frame.png";
            Emote2.Source = "Achievement_Frame.png";
            Emote3.Source = "Achievement_Frame.png";
            Emote4.Source = "Achievement_Frame.png";
            Emote5.Source = "Achievement_Frame.png";
        }

        async void Button_Clicked(object sender, EventArgs e)
        {
            var result = await MediaPicker.CapturePhotoAsync(new MediaPickerOptions
            {
                Title = "please pick a photo"
            });
            var stream = await result.OpenReadAsync();
            resultImage.Source = ImageSource.FromStream(() => stream);
        }
        public ObservableCollection<Image> selectedImages { get; set; }
        public ObservableCollection<Image> SelectedImages
        {
            get { return selectedImages; }
            set
            {
                selectedImages = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName,string propertyName1)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName1));
        }


    private string _image1;
    private string _image2;
    private string _image3;
    private string _image4;
    private string _image5;
        public string Image1
        {
            set
            {
                if (_image1 != value)
                {
                    _image1 = value;
                    OnPropertyChanged("Image1");
                }
            }
            get
            {
                return _image1;
            }
        }
        public string Image2
        {
            set
            {
                if (_image2 != value)
                {
                    _image2 = value;
                    OnPropertyChanged("Image2");
                }
            }
            get
            {
                return _image2;
            }
        }
        public string Image3
        {
            set
            {
                if (_image3 != value)
                {
                    _image3 = value;
                    OnPropertyChanged("Image2");
                }
            }
            get
            {
                return _image3;
            }
        }
        public string Image4
        {
            set
            {
                if (_image4 != value)
                {
                    _image4 = value;
                    OnPropertyChanged("Image2");
                }
            }
            get
            {
                return _image4;
            }
        }
        public string Image5
        {
            set
            {
                if (_image5 != value)
                {
                    _image5 = value;
                    OnPropertyChanged("Image2");
                }
            }
            get
            {
                return _image5;
            }
        }
        public Profile(EmotesModel emotesModel)
        {
            InitializeComponent();
            Image1 = emotesModel.Image;
            Image2 = emotesModel.Image;
            Image3 = emotesModel.Image;
            Image4 = emotesModel.Image;
            Image5 = emotesModel.Image;
            BindingContext = this;
        }




        public void ImageButton_Clicked(object sender, EventArgs e)
        {
            Navigation.PushAsync(new EmotesPage());



        }

        public void ImageButton_Clicked_1(object sender, EventArgs e)
        {
            Navigation.PushAsync(new EmotesPage());
        }

        public void ImageButton_Clicked_2(object sender, EventArgs e)
        {
            Navigation.PushAsync(new EmotesPage());
        }

        public void ImageButton_Clicked_3(object sender, EventArgs e)
        {
            Navigation.PushAsync(new EmotesPage());
        }

        public void ImageButton_Clicked_4(object sender, EventArgs e)
        {
            Navigation.PushAsync(new EmotesPage());

        }
    

    }
}
Developer technologies | .NET | Xamarin
Developer technologies | Visual Studio | Other
Developer technologies | XAML
{count} votes

Accepted answer
  1. Anonymous
    2023-02-23T09:07:14.9933333+00:00

    Hello,

    Do you want to transfer data when EmotesPage page navigate back to the Profile page?

    If so, you can do this by using event call back.

    Firstly, we can create a CustomEventArgs for event call back.

    public class CustomEventArgs : EventArgs
        {
            private readonly string customString;
            public CustomEventArgs(string customString)
            {
                this.customString = customString;
            }
            public string CustomString
            {
                get { return this.customString; }
            }
        }
    

    Then open your EmotesPage page, add call back event, for example, when execute EmotesView_SelectionChanged_1, you want to transfer image's source to the Profile page like following EmotesView_SelectionChanged_1, you can use  Navigation.PopAsync(); to navigate back page. And send data to the Profile page by  NaviBackEvent?.Invoke(this, new CustomEventArgs(EmoteSelected.Image));.

     public partial class EmotesPage : ContentPage
        {
            public event EventHandler<CustomEventArgs> NaviBackEvent;
            private void OnNaviBackEvent(string image)
            {
                if (NaviBackEvent != null)
                {
                    NaviBackEvent(this, new CustomEventArgs(image));
                }
            }
    
      private async void EmotesView_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
            {
                var EmoteSelected = e.CurrentSelection[0] as EmotesModel; 
               Profile EmoteClicked = new Profile(EmoteSelected);
                     await Navigation.PopAsync();
                NaviBackEvent?.Invoke(this, new CustomEventArgs(EmoteSelected.Image));
            }
    

    Next, open your Profile page, I make a test in the Emote1 button click event, when navigate back, change the select image for imagebutton like following code.

    public void ImageButton_Clicked_1(object sender, EventArgs e)
            {
                ImageButton imageButton= sender as ImageButton;
                EmotesPage emotesPage = new EmotesPage();
                emotesPage.NaviBackEvent +=(NaviSender,CustomEventArgs)=>{
                    Emote1.Source = CustomEventArgs.CustomString;
                };
                Navigation.PushAsync(emotesPage);
                     }
    

    Please note, if you use Navigation.PushAsync, you have to wrap the NavigationPage in the App.xaml.cs.

    public App()
            {
                InitializeComponent();
                MainPage =new NavigationPage( new EmotesPage());
            }
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.


0 additional answers

Sort by: Most helpful

Your answer

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