Xamarin - Observablecollections between 3 forms

0

I have a developing application with a serious problem in passing data from one screen to another. When I started, I made a start screen with a listview, in it I had a searchbox where the user typed a brief description of a vehicle and clicking on the search button, the system returned the data, triggering the icommand, causing a viewmodel to process data and by the observablecollection displayed in the listview. Now I had to implement an advanced search screen, I have not put any code to filter it, I just put the fetch button that executes the same icommand, I make a debug, the variable that receives the list of data is populated, however the listview continues without anything.

I'm almost desperate because of this, my knowledge of C # is minimal, and I'm new to Visual Studio, to make matters worse.

If someone can help me, thank you.

Follow the first screen

namespace TvCar.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class PesquisaDetalhada : ContentPage, INotifyPropertyChanged
{
    public PesquisaDetalhada()
    {
        InitializeComponent();

        this.BindingContext = new ViewModels.Veiculo_ViewModel();            

        string img_condicao, img_marca, img_modelo, img_ano, img_versao, img_preco, img_km, img_cambio, img_combustivel, img_portas, img_cor, img_cidade;

        switch (Device.RuntimePlatform)
        {
            case Device.iOS:
                img_condicao    = "img_condicao.png";
                img_marca       = "img_marca.png";
                img_modelo      = "img_modelo.png";
                img_ano         = "img_ano.png";
                img_versao      = "img_versao.png";
                img_preco       = "img_preco.png";
                img_km          = "img_km.png";
                img_cambio      = "img_cambio.png";
                img_combustivel = "img_combustivel.png";
                img_portas      = "img_portas.png";
                img_cor         = "img_cor.png";
                img_cidade      = "img_cidade.png";
                break;

            case Device.Android:
            default:
                img_condicao = "img_condicao.png";
                img_marca = "img_marca.png";
                img_modelo = "img_modelo.png";
                img_ano = "img_ano.png";
                img_versao = "img_versao.png";
                img_preco = "img_preco.png";
                img_km = "img_km.png";
                img_cambio = "img_cambio.png";
                img_combustivel = "img_combustivel.png";
                img_portas = "img_portas.png";
                img_cor = "img_cor.png";
                img_cidade = "img_cidade.png";
                break;
        }

        listView.ItemsSource = new List<Item>
        {
            new Item {Name = "Condição",Image = img_condicao} ,
            new Item {Name = "Marca",Image = img_marca} ,
            new Item {Name = "Modelo",Image = img_modelo},
            new Item {Name = "Ano",Image = img_ano},
            new Item {Name = "Versão",Image = img_versao},
            new Item {Name = "Preço",Image = img_preco},
            new Item {Name = "KM",Image = img_km},
            new Item {Name = "Cambio",Image = img_cambio},
            new Item {Name = "Combustível",Image = img_combustivel},
            new Item {Name = "Portas",Image = img_portas},
            new Item {Name = "Cor",Image = img_cor},
            new Item {Name = "Cidade",Image = img_cidade}
        };
    }

    private async void Button_Clicked(object sender, EventArgs e)
    {
        await App.Current.MainPage.Navigation.PushAsync(new Views.ListarAnuncios());
    }

    class Item
    {
        public string Name { get; set; }
        public string Image { get; set; }
    }
}

Follow the second screen (Listview)

namespace TvCar.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ListarAnuncios : INotifyPropertyChanged
{
    public ListarAnuncios()
    {
        InitializeComponent();            

        this.Detalhes.ItemsSource = null;
        this.Detalhes.ItemsSource = new ObservableCollection<ViewModels.Veiculo_ViewModel>();

        this.Detalhes.ItemTapped += async (sender, e) =>
        {
            var detalhe = e.Item as Model.Veiculo_Cadastro;
            await App.Current.MainPage.Navigation.PushAsync(new Views.VeiculoDetalhes(detalhe));

            MainPage.mcodveiculo = detalhe.VEICULO_CODIGO;
            MainPage.mDesVeiculo = detalhe.VEICULO_DESCRICAO;
            MainPage.Atualiza_Visitas(sender: PropertyChanged);
        };
    }

    internal static ListView Listarlv;

    //private void OnTextChanged()
    //{
    //    MainPage.DadosBusca = string.Concat("AND UPPER(A.VEICULO_VERSAO) LIKE UPPER('%", Pesquisar.Text, "%') ");
    //}

    public class StringCaseConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {

            string param = System.Convert.ToString(parameter) ?? "u";

            switch (param.ToUpper())
            {
                case "U":
                    return ((string)value).ToUpper();
                case "L":
                    return ((string)value).ToLower();
                default:
                    return ((string)value);
            }

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public VeiculoDetalhes Selecao_Item { get; set; }
}

Follow the class that searches the data

namespace TvCar.ViewModels
{
public sealed class Veiculo_ViewModel : INotifyPropertyChanged // classe
{
    public ICommand CarregarCommand { get; set; }
    public VeiculoDetalhes Selecao_Item { get; set; }        

    private ObservableCollection<Model.Veiculo_Cadastro> Anuncio;
    public ObservableCollection<Model.Veiculo_Cadastro> Anuncios
    {
        get { return Anuncio; }
        set
        {
            Anuncio = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Anuncios)));
        }
    }

    public Veiculo_ViewModel()
    {
        CarregarCommand = new Xamarin.Forms.Command(async () =>
        {
            var Anuncio = await ApiAnuncios.Api_Rest.GetAsync();
            Anuncios = new ObservableCollection<Model.Veiculo_Cadastro>(Anuncio);


            TvCar.MainPage.MinhaLista = new ListView();
            TvCar.MainPage.MinhaLista.ItemsSource = null;
            TvCar.MainPage.MinhaLista.ItemsSource = Anuncios;
        });
    }

    public event PropertyChangedEventHandler PropertyChanged;
}
    
asked by anonymous 10.05.2018 / 17:20

1 answer

0

Good day, after a lot of headache, I ended up finding a solution, maybe not ideal, but it worked for me. I made the following changes: In my MainPage.cs I created a static listview like this: public static ListView MyList = null;

Inside the Vehicle_ViewModel Before -------------- > public Veiculo_ViewModel ()         {             LoadCommand = new Xamarin.Forms.Command (async () = >             {                 var Advertisement = await ApiAnuncios.Api_Rest.GetAsync ();                 Announcements = new ObservableCollection (Announcement);             });         }

Now ------------ > public Veiculo_ViewModel ()         {             LoadCommand = new Xamarin.Forms.Command (async () = >             {                 var Advertisement = await ApiAnuncios.Api_Rest.GetAsync ();                 Announcements = new ObservableCollection (Advertisement);

            MainPage.MinhaLista = new ListView();
            MainPage.MinhaLista.ItemsSource = Anuncios;
        });
    }

Inside the Adslist.cs Before ------ > this.Detalhes.ItemsSource = new ObservableCollection ();

Now ------- > this.Detalhes.ItemsSource = MainPage.MyList.ItemsSource;

Inside the Adslist.xaml Before ---------- >

Now ----------- >

Within the DetailedSearch.cs Before ----------------- >         private async void Button_Clicked (object sender, EventArgs and)         {             await App.Current.MainPage.Navigation.PushAsync (new Views.ListAds ());         }

Now -------------- >         private async void Button_Clicked (object sender, EventArgs and)         {             ReloadItems ();
        }

    public async Task CarregaItens()
    {
        var xAnuncio = new ViewModels.Veiculo_ViewModel();
        await App.Current.MainPage.Navigation.PushAsync(new Views.ListarAnuncios());
    }

Aha, it worked wonderfully. Stay the hint.

    
11.05.2018 / 14:49