Xamarin: Pass data from selected item in a Listview to another Screen

Asked

Viewed 506 times

1

How do I pass selected data from one screen to another in Xamarin?

I have a Listview that receives the information from an Api and I want the user to click on the item, view this information on another screen, below the click code

   private async void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
                if(e.SelectedItem !=null)
                {
                    var selection = e.SelectedItem as UltimasNoticias;
                    //DisplayAlert("Você Selecionou", selection.Post_title, "ok");                         
                    await Navigation.PushAsync(new PostView());
                    #region DisabledSelectionHighlighting
                    // ((ListView)sender).SelectedItem = null;
                    #endregion
                } 

     }

1 answer

1


You can pass them through the page builder, for example:

private async void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
{
    if(e.SelectedItem !=null)
    {
        var selection = e.SelectedItem as UltimasNoticias;
        await Navigation.PushAsync(new PostView(selection));
        #region DisabledSelectionHighlighting
        ((ListView)sender).SelectedItem = null;
        #endregion
    } 
}

In codebehind you could treat so:

public class PostView : ContentPage
{
    PostViewModel viewModel = null;

    public PostView() : this(new UltimasNoticias())
    {

    }

    public PostView(UltimasNoticias dados)
    {
        InitializeComponents();

        // Aqui você usa o parâmetro para entregar para sua ViewModel ou o que quer que seja
        viewModel = new PostViewModel(dados);

        BindingContext = viewModel;
    }
}

I hope it helps.

  • 1

    valeu helped thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.