ListView Item Xamarin Forms being opened 2 times Custom Controls

1

When viewing my list and clicking on an item, it opens 2 times the same page as the item.

public class DeliveryViewModel : BaseViewModel
{
    public ObservableCollection<Delivery> Delivery_Col { get; }
    public Command<Delivery> ShowDeliveryCommand { get; }
    Delivery_lib delivery_lib = new Delivery_lib();

    public DeliveryViewModel()
    {
        Delivery_Col = new ObservableCollection<Delivery>();

        ShowDeliveryCommand = new Command<Delivery>(ExecuteShowDeliveryCommand);

        Task.Run(() => this.LoadAsync()).Wait();

        Title = "Entregas";
    }

    private async void ExecuteShowDeliveryCommand(Delivery delivery)
    {
        try
        {
            await PushAsync<MapViewModel>(delivery);
        }catch(Exception ex)
        {
            await DisplayAlert("Erro", Convert.ToString(ex), "OK", "Cancelar");
            Debug.WriteLine(ex);
        }
    }

It can be seen debugging the code that the await PushAsync (delivery); command runs twice, only in lists.

public class BaseViewModel
{

    private string _title;
    public string Title
    {
        get { return _title; }
        set { SetProperty(ref _title, value); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName]string propertyName = null)
    {
        if (EqualityComparer<T>.Default.Equals(storage, value))
        {
            return false;
        }

        storage = value;
        OnPropertyChanged(propertyName);

        return true;
    }

    public async Task PushAsync<TViewModel>(params object[] args) where TViewModel : BaseViewModel
    {
        var ViewModelType = typeof(TViewModel);

        //pega o nome da classe view model e troca a parte do viewmodel para Page, onde faz o acesso a pagina
        var ViewModelTypeName = ViewModelType.Name;
        var ViewModelWorldLength = "ViewModel".Length;

        //subtrai do nome da pagina o ViewModel e troca por page
        var ViewTypeName = $"AppFrete.View.Details.{ViewModelTypeName.Substring(0, ViewModelTypeName.Length - ViewModelWorldLength)}Page";
        var ViewType = Type.GetType(ViewTypeName);

        //chama o construtor da pagina
        var page = Activator.CreateInstance(ViewType) as Page;


        var viewModel = Activator.CreateInstance(ViewModelType, args);
        if (page != null)
        {
            page.BindingContext = viewModel;
        }
        await Application.Current.MainPage.Navigation.PushAsync(page);
    }

My custom list

public class MyListView : ListView
{
    public static readonly BindableProperty ItemTappedCommandProperty = //Declaração da Propriedade
        BindableProperty.Create("ItemTappedCommand", // Criando o nome
            typeof(ICommand), //Tipo de retorno
            typeof(MyListView), //Propriedade pai
            null); //Valor inicial

    public ICommand ItemTappedCommand
    {
        get { return (ICommand)GetValue(ItemTappedCommandProperty); }
        set
        {
            SetValue(ItemTappedCommandProperty, value);
        }
    }

    public MyListView(ListViewCachingStrategy strategy) : base(strategy)
    {
        Initialize();
    }

    public MyListView() : this(ListViewCachingStrategy.RecycleElement)
    {
        Initialize();
    }

    private void Initialize()
    {
        this.ItemSelected += (sender, e) =>
        {
            if ((ItemTappedCommand != null) && (ItemTappedCommand.CanExecute(e.SelectedItem)))
            {
                ItemTappedCommand.Execute(e.SelectedItem);
            }
        };
    }
}
    
asked by anonymous 06.06.2018 / 01:25

1 answer

0

Problem in custom list, I removed one of the Initialize (); and it worked normal

 public MyListView(ListViewCachingStrategy strategy) : base(strategy)
{
    Initialize();
}

public MyListView() : this(ListViewCachingStrategy.RecycleElement)
{
    //Initialize();
}
    
06.06.2018 / 01:56