Posted on Leave a comment

Override hardware back button Xamarin.Forms

Some phones support the hardware back button and this may disturb the designed navigation experience that has been implemented in the application. You can override this behavior on Android (iOS does not support a hardware back button) with the following code that has to be placed in the MainActivity.cs of your Android root folder after onCreate method.

 public override void OnBackPressed()
        {
           return;
        }

The result should look like below. After this action the hardware back button will have no effect and the whole navigation can be performed through the application.

Posted on 1 Comment

Consume php backend nested array on a C# Application – Json.NET

Lets assume you got a MYSQL database that contains data for your application and you want to consume this information on a C# Application in order to handle it on a client.

For the sake of this example the following table will be parsed

As depicted the following information is returned from the api:

  • the state of the response (succeeded or not)
  • a message that will show the response result
  • the actual result that in this case is a list of trips

Each trip contains id, fromlocation, tolocation, date and time properties that have to be returned to the application.

The php code that was used to create the data from the database is the following:

         $trips =array();
         while ( $row = $stmt->fetch() ) {         
         $obj["id"]= $row['id'];
         $obj['fromlocation']= $row['fromlocation'];
         $obj['tolocation'] = $row['tolocation'];       
         $obj['date']= $row['date'];
         $obj['time'] = $row['time'];         
         array_push($trips,$obj);
         }

         $response["success"] = 1;
         $response["message"] = "Trips returned successfully!";
         $response["trips"] = $trips;
         die(json_encode($response));

So in order to parse this json result we will need a plugin like Newtonsoft.Json

The class that will be used to encapsulate the response would be the following:

public class GetTrips
    {
        [JsonProperty("success")]
        public int Success { get; set; }

        [JsonProperty("message")]
        public string Message { get; set; }
        [JsonProperty("trips")]
        public Trip[]  Trips { get; set; }

       
        public class Trip
        {
            [JsonProperty("id")]
            public string Id { get; set; }
            [JsonProperty("fromlocation")]
            public string FromLocation { get; set; }
            [JsonProperty("tolocation")]
            public string ToLocation { get; set; }
            [JsonProperty("date")]
            public string Date { get; set; }
            [JsonProperty("time")]
            public string Time { get; set; }
        }

    }

And then we will consume the json response with the following code in C#. In this example data from the API came from a POST request on a web Form.

public async TaskGetTripsAsync(string username,string password)
        {
            try
            {
                var keyValues = new Liststring, string>>
                {
                    new KeyValuePair("username",username),
                    new KeyValuePair("password",password)
                };

                var request = new HttpRequestMessage(HttpMethod.Post, Url);
                request.Content = new FormUrlEncodedContent(keyValues);

                var client = new HttpClient();
                var result = await client.SendAsync(request);
                var content = await result.Content.ReadAsStringAsync();
                var tripsResult = JsonConvert.DeserializeObject (content);
                return tripsResult;
            }
            catch (Exception)
            {
                return null;
            }
        }