Xamarin - Observablecollections between 3 Forms

Asked

Viewed 79 times

0

I have an app in development with a serious problem in passing data from one screen to another. When I started, I made a home screen with a listview, in it I had a searchbox where the user typed a brief description of a vehicle and when clicking the search button, the system returned the data, triggering the icommand, causing a viewmodel to process the data and the observablecollection showed in the listview. Now I had to implement an advanced search screen, I haven’t put any code to filter on it, I just put the search button that runs the same icommand, when I debug, the variable that receives the data list is populated, but the listview continues with nothing.

I’m almost in despair because of this, my knowledge of C# is minimal, and I’m new to Visual Studio, to make it worse.

In case anyone can help me, I’d appreciate it.

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 data search class

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;
}
  • 1

    Without the codes to know exactly where is the problem gets hard to help. It would be nice to post yes.

  • So, Gustavo, here follow the screens. Using the listview screen, with a Searchbar, the icommand of the vehicle class is triggered and returns the data in the listview. When I click the search button of the Listings screen, it also triggers the icommand of the vehicle class_viewmodel is triggered, the variable Ads is loaded with the data, but the listview does not receive data.

1 answer

0

Good morning, after hitting my head a lot, I found a solution, maybe not ideal, but it worked for me. I made the following modifications: In my Mainpage.Cs I created a static listview like this: public Static Listview Minhalista = null;

Inside the Vehicle Before ------------------> public Veiculo_viewmodel() { Carregarcommand = new Xamarin.Forms.Command(async() => { var Anuncio = await Apianuncios.Api_rest.Getasync(); Announcements = new Observablecollection(Announcement); }); }

Now ----------------------------------------------- public Veiculo_viewmodel() { Carregarcommand = new Xamarin.Forms.Command(async() => { var Anuncio = await Apianuncios.Api_rest.Getasync(); Announcements = new Observablecollection(Announcement);

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

Within the Listingterms.Cs Before ------> this.Details.Itemssource = new Observablecollection();

Now --------> this.Details.Itemssource = Mainpage.MinhaLista.Itemssource;

Inside the Listaranuncios.xaml Before ---------->

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

Inside the Detailed Search.Cs Before ----------------------> private async void Button_clicked(Object Sender, Eventargs and) { await App.Current.Mainpage.Navigation.Pushasync(new Views.Listings()); }

Now ----------------------------------------------- private async void Button_clicked(Object Sender, Eventargs and) { Upload items();
}

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

Yeah, it worked great. Stay and take a hint.

Browser other questions tagged

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