Listview Item Xamarin Forms being opened 2 times Custom Controls

Asked

Viewed 58 times

1

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

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 is understood by debugging the code that the line with the command await Pushasync< Mapviewmodel/>(delivery); is executed 2 times, 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 customized 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);
            }
        };
    }
}

1 answer

0


Problem in custom list, removed one of Boot(); and worked normal

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

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

Browser other questions tagged

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