如何在c#Xamarin

时间:2017-05-11 18:07:46

标签: c# android json button xamarin

您好我有以下代码:

using System;
using Android.App;
using Android.Widget;
using Android.OS;
using RestSharp;
using Newtonsoft.Json;
using Android.Util;
using Newtonsoft.Json.Linq;
using Org.Json;
using System.Net;
using System.IO;
using System.Collections.Generic;
using System.Globalization;
using App1;
using App1.Resources;

namespace App1
{
[Activity(Label = "App1", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
    EditText edtcpf;
    TextView txtnome;
    TextView txtorder;
    TextView txtsobrenome;
    TextView txtcpf;
    RestClient orderId { get; set; }
    RestClient consumer { get; set; }
    RestRequest cpf { get; set; }
    RestRequest requestorderId { get; set; }
    IRestResponse answerorder { get; set; }
    IRestResponse mensagemConsumer;
    Button btnConsumer;
    RadioButton cancelada;
    RadioButton aberta;
    RadioButton agendada;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        edtcpf = FindViewById<EditText>(Resource.Id.edtcpf);
        txtcpf = FindViewById<TextView>(Resource.Id.txtcpf);
        txtsobrenome = FindViewById<TextView>(Resource.Id.txtresposta);
        txtnome = FindViewById<TextView>(Resource.Id.txtNome);
        txtorder = FindViewById<TextView>(Resource.Id.txtorder);
        btnConsumer = FindViewById<Button>(Resource.Id.btnConsumer);
        cancelada = FindViewById<RadioButton>(Resource.Id.rdbtnCancelada);
        aberta = FindViewById<RadioButton>(Resource.Id.rdbtnAberta);
        agendada = FindViewById<RadioButton>(Resource.Id.rdbtnAgendada);
        btnConsumer.Click += BtnConsumer_Click;

    }

    private void BtnConsumer_Click(object sender, EventArgs e)
    {
        try
        {
            // API Consumer CPF

            consumer = new RestClient("https://qa.api-latam.whirlpool.com/v1.0/consumers");
            cpf = new RestRequest("/" + edtcpf.Text, Method.GET);
            cpf.AddHeader("Content-Type", "application/json; charset=utf-8");
            cpf.AddHeader("Authorization", "Bearer 70197e6c-d81b-384c-bb32-d69e8c10b101");
            mensagemConsumer = consumer.Execute(cpf);
            Pessoa pessoa = JsonConvert.DeserializeObject<Pessoa>(mensagemConsumer.Content);
            txtnome.Text = "Nome: " + pessoa.firstName;
            txtsobrenome.Text = "Sobrenome: " + pessoa.lastName;

            // API Consumer Appliances
            orderId = new RestClient("https://qa.api-latam.whirlpool.com/v1.0/consumers/");
            requestorderId = new RestRequest("/" + edtcpf.Text + "/service-orders", Method.GET);
            requestorderId.AddHeader("Content-Type", "application/json; charset=utf-8");
            requestorderId.AddHeader("Authorization", "Bearer 70197e6c-d81b-384c-bb32-d69e8c10b101");
            answerorder = orderId.Execute(requestorderId);
            var requestToken = JsonConvert.DeserializeObject<RootObject>(answerorder.Content);
            var parse = JObject.Parse(answerorder.Content);
            var QtdeItens = parse.Count;
            foreach (var order in requestToken.orders)
            {
                object vader = order.order.orderId;
                string darth = Convert.ToString(vader);
                string hora = order.order.orderOpeningDate;
                DateTime dia = Convert.ToDateTime(hora);
                txtorder.Text = "OrderId: " + darth + "ServiceProviderId: " + order.order.serviceProviderId + "Data de abertura: " + order.order.orderOpeningDate;

               if (order.order.orderStatusCode == "CANC")
                {
                    cancelada.Enabled = true;
                    agendada.Enabled = false;
                    aberta.Enabled = false;
                }

                else
                    if (order.order.orderStatusCode.Equals("ABRT"))
                {
                    cancelada.Enabled = false;
                    agendada.Enabled = false;
                    aberta.Enabled = true;

                }

                else
                    if (order.order.orderStatusCode.Contains("AGEN"))
                {
                    cancelada.Enabled = false;
                    agendada.Enabled = true;
                    aberta.Enabled = false;
                }
            }

哪一个是正确的?

        }
        catch (Exception)
        {

            throw;
        }




    }


}

}

我试图启用一个按钮,这取决于Json答案所具有的状态。它可以是:&#34;已关闭&#34;,&#34; CANC&#34;,&#34; ABRT&#34;和#34; AGEN&#34;。我创建了3个radiobuttons,如果Json答案是CANC,它会显示一个按钮,如果是ABRT,它会显示第二个按钮,如果它是AGEN,则应用程序会显示第三个按钮。

我试过了:

if (order.order.orderStatusCode == "CANC"){
       cancelada.Enabled = true;
       agendada.Enabled = false;
       aberta.Enabled = false;
 } 

但它没有用,所以我怎么能这样做?enter image description here

2 个答案:

答案 0 :(得分:1)

给它一个机会。通过调试找出实际传递的值。

Debug.Log(order.order.orderStatusCode)
if (order.order.orderStatusCode == "CANC")
   {
    cancelada.Enabled = true;
    gendada.Enabled = false;
    aberta.Enabled = false;
   }
else if (order.order.orderStatusCode == "ABRT")
   {
    cancelada.Enabled = false;
    agendada.Enabled = false;
    aberta.Enabled = true;
    }
else if (order.order.orderStatusCode == "AGEN")
    {
     cancelada.Enabled = false;
     agendada.Enabled = true;
     aberta.Enabled = false;
    }

答案 1 :(得分:1)

如果您有3个单选按钮,并且在每个场景中禁用2,那么使用单选按钮是没有意义的。

  

我启用一个按钮来显示它并禁用其他按钮

我认为你想要隐藏/显示按钮,但却错误地使用了这些术语。如果是这种情况,那么您应该使用IsVisible属性而不是Enabled属性,例如

if (order.order.orderStatusCode == "CANC")
{
    cancelada.IsVisible = true;
    gendada.IsVisible = false;
    aberta.IsVisible = false;
}

记住

  • IsVisible用于表示是否显示可视元素
  • Enabled用于指示可视元素是否接受用户输入。对于按钮,这意味着能够点击它,对于文本框,这意味着能够在其中输入文本。

让我知道这是否有效!