Xamarin: Pass data from the selected item in a ListView to another screen

1

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

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

   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
                } 

     }
    
asked by anonymous 03.02.2018 / 23:25

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 like this:

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.

    
06.02.2018 / 20:45