C# Xamarin.Forms

Asked

Viewed 40 times

0

I’m looking for a way to, in a record, correlate state with city through Picker selection. Does anyone help? It’s for Xamarin Forms. Here are the codes:

        <Entry x:Name="EntryTelefone" Placeholder="Telefone" />
        <Entry x:Name="EntryEmail" Placeholder="Email" />
        <Button Text="Salvar" Clicked="SalvarCadastroAction"/>
    </StackLayout>
</ContentPage.Content>

using System;
using EstudoSQLite.Modelos;
using EstudoSQLite.Banco;

using Xamarin.Forms;
using System.Net.Http;

namespace EstudoSQLite.Paginas
{
    public partial class CadastroVaga : ContentPage
    {
        public CadastroVaga()
        {
            InitializeComponent();
        }
        public async void SalvarCadastroAction(object senders, EventArgs args)
        {

            //Cada TODO é uma tarefa a ser implementada


            /*TODO - Obter dados da tela
             */
            //Pra esse, primeiro, se faz o using EstudosSQLite.Modelos e depois se cria a variavel vaga
            Vaga vaga = new Vaga();
            vaga.Nomevaga = EntryVagaCargo.Text;
            vaga.Quantidade = short.Parse(EntryQuantidade.Text); //Porque quantidade é um numero, nao string
            vaga.Salario = double.Parse(EntrySalario.Text);
            vaga.Empresa = EntryNomeEmpresa.Text;
            vaga.Cidade = EntryCidade.Text;
            vaga.Descricao = EntryDescricao.Text;
            //Se for true, é PJ (direita). Do contrario, é na esquerda
            vaga.TipoContratacao = (EntrySwitch.IsToggled) ? "PJ" : "CLT";
            //Nenhum dos dois abaixo foi validado
            vaga.Telefone = EntryTelefone.Text;
            vaga.Email = EntryEmail.Text;

            //Validando a cidade:
            HttpClient client = new HttpClient();
            HttpResponseMessage response = await client.GetAsync("https://servicodados.ibge.gov.br/api/v1/localidades/distritos?orderBy=nome");
            response.EnsureSuccessStatusCode();
            string responseBody = await response.Content.ReadAsStringAsync();
            //Se o dado retornado for json, utilize o JsonConvert.DeserializeObject<TIPO>();

            /*TODO - Salvar informacoes no banco
             */
            //Primeiro se chama a classe AcessoBanco (usar using EstudosSQLite.Banco)
            AcessoBanco acessobanco = new AcessoBanco();
            //Agora se faz o uso do metodo de cadastro:
            acessobanco.Cadastro(vaga);

            /*TODO - Voltar para tela pesquisa
             */
            //vai voltar pra tela anterior com o PopAsync()
            Navigation.PopAsync();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Net.Http;
using Xamarin.Forms;

namespace EstudoSQLite
{
    public partial class Picker_cidade_e_estado : ContentPage
    {
        public Picker_cidade_e_estado()
        {
            InitializeComponent();
        }
        private async void ActionMudarEstado(object senders, EventArgs args)
        {
            Picker obj = (Picker)senders;
            
            //Validando o estado:
            HttpClient client = new HttpClient();
            HttpResponseMessage response = await client.GetAsync("https://servicodados.ibge.gov.br/api/v1/localidades/estados?orderBy=nome");
            response.EnsureSuccessStatusCode();
            string responseBody = await response.Content.ReadAsStringAsync();
            obj.SelectedItem = responseBody;
        }
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="EstudoSQLite.Picker_cidade_e_estado">
    <ContentPage.Content>
        <StackLayout>
            <Picker x:Name="Cidade" SelectedIndexChanged="ActionMudarEstado">
                <Picker.Items>
                    <x:String>AC</x:String>
                    <x:String>AL</x:String>
                    <x:String>AM</x:String>
                    <x:String>BA</x:String>
                    <x:String>CE</x:String>
                    <x:String>DF</x:String>
                    <x:String>ES</x:String>
                    <x:String>GO</x:String>
                    <x:String>MA</x:String>
                    <x:String>MG</x:String>
                    <x:String>MS</x:String>
                    <x:String>MT</x:String>
                    <x:String>PA</x:String>
                    <x:String>PB</x:String>
                    <x:String>PE</x:String>
                    <x:String>PI</x:String>
                    <x:String>PR</x:String>
                    <x:String>RJ</x:String>
                    <x:String>RN</x:String>
                    <x:String>RO</x:String>
                    <x:String>RR</x:String>
                    <x:String>RS</x:String>
                    <x:String>SC</x:String>
                    <x:String>SE</x:String>
                    <x:String>SP</x:String>
                    <x:String>TO</x:String>
                </Picker.Items>
            </Picker>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>
  • What exactly is your doubt?

  • I am trying, through httpClient and Picker, to make that when the person selects a state among the items of Picker, automatically already come the cities available in that state for her to select.

  • And what is the doubt? Where is the requisition of cities by State? Just take this return, clean and popular the second Picker...

  • My doubt is that I can not popular the second Picker nor filter the cities of the selected state

  • Why can’t you? Ask yourself this question and attack each of the blocks at a time

No answers

Browser other questions tagged

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