question

AwesomeScottB-3078 avatar image
0 Votes"
AwesomeScottB-3078 asked LeonLu-MSFT edited

Inserting value into Entry from ListView

Hello

I am doing a test case. The bottom line is something like this: when a button is pressed on the MainPage, a page is called, for example, essaywriterfree (Other Page), which has a ListView. The question arose, how is the value selected in the ListView on the OtherPage by pressing the button to insert into the Entry on the MainPage?
I would be very grateful for a hint.


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

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT edited

Hello,​

Welcome to our Microsoft Q&A platform!

The question arose, how is the value selected in the ListView on the OtherPage by pressing the button to insert into the Entry on the MainPage?


You can use MessagingCenter to achieve it. When value selected in the ListView on the OtherPage, you can use MessagingCenter to transfer data from OtherPage to MainPage.


public partial class Page1 : ContentPage
    {
        ObservableCollection<Employee> employees = new ObservableCollection<Employee>();
        public ObservableCollection<Employee> Employees { get { return employees; } }
        public Page1()
        {
            InitializeComponent();
            EmployeeView.ItemsSource = employees;

           
            employees.Add(new Employee { DisplayName = "Rob Finnerty" });
            employees.Add(new Employee { DisplayName = "Bill Wrestler" });
            employees.Add(new Employee { DisplayName = "Dr. Geri-Beth Hooper" });
            employees.Add(new Employee { DisplayName = "Dr. Keith Joyce-Purdy" });
            employees.Add(new Employee { DisplayName = "Sheri Spruce" });
            employees.Add(new Employee { DisplayName = "Burt Indybrick" });
        }

        private void EmployeeView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            Employee employee= e.SelectedItem as Employee;

           //push current page
            Navigation.PopModalAsync();
           //send data to previous page.
            MessagingCenter.Send<App, Employee>((App)App.Current, "OneMessage", employee);

          
        }
    }


    public class Employee
    {
        public string DisplayName { get; set; }
    }
}


In the Mainpage.cs, we can receive it from MessagingCenter.Subscribe.

public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();

            MessagingCenter.Subscribe<App, Employee>(App.Current, "OneMessage", (snd, arg) =>
            {
                Device.BeginInvokeOnMainThread(() => {
                
                 Employee employee = arg as Employee;
                 SelectValue.Text = employee.DisplayName;
                });
            });
        }

        private void Button_Clicked(object sender, EventArgs e)
        {
            Navigation.PushModalAsync(new Page1());
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();

        }
    }
}

Here is running screenshot.

83837-image.png

83830-image.png

Best Regards,

Leon Lu



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.



image.png (12.4 KiB)
image.png (20.6 KiB)
· 1
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.

@AwesomeScottB Did you test above code in your project, is that worked? If you do not want to achieve it by above way, you can test with MVVM to achieve it as well.

0 Votes 0 ·