1

Ao exibir minha lista e clicar em cima de um item, o mesmo abre 2 vezes a mesma página do 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);
        }
    }

Percebe-se debugando o código que a linha com o comando await PushAsync<\MapViewModel/>(delivery); é executada 2 vezes, somente em listas.

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);
    }

Minha lista customizada

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 Resposta 1

0

Problema na lista personalizada, retirei um dos Inicialize(); e funcionou normal

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

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

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .