Home > Back-end >  Refresh Page with new data from APi by searchHandler Xamarin C#
Refresh Page with new data from APi by searchHandler Xamarin C#

Time:11-22

I have a question about the function search Handler in xamarin. I am just starting to learn xamarin, and I am trying in my project with search handler in xamarin to refresh my page with new data from the API. Currently, I am already lucky to retrieve the data, but when I do this, it creates a new page so to speak, but this is not what I want. He would kind of reload the page with new data. I have also already tried to delete previous page with "Shell.Current.Navigation.PopAsync();" But with no residual result. Anyone knows how I can achieve what I want? In addition, I would also like to remove that blur you get after the search. Thanks in advance!

using Eindproject.Models;
using Eindproject.Repository;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Eindproject.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Weather : ContentPage
    {
        private string icao = "EBBR";


        public Weather()
        {
            InitializeComponent();
        }

        public Weather(string icao)
        {
            InitializeComponent();
            this.icao = icao;
            frontend(icao);
        }

        public async void frontend(string par_icao)
        {
            // Get weather
            Models.WeatherModel weather = await DataRepository.GetWeatherAsync(par_icao);
            // Set data to labels
            lblLocation.Text = weather.Station.Name;
            lblCode.Text = weather.Code;
            lblTemp.Text = weather.Temperature.C.ToString();
            lblHumidity.Text = weather.Humidity.Percent.ToString();
            lblWind.Text = weather.Wind.Degrees.ToString();
            lblPressure.Text = weather.Presure.Hpa.ToString();
            lblDate.Text = weather.Date.ToString("G");
            lblMetar.Text = weather.Metar;
            lblCloud.Text = weather.Clouds[0].text;

            // Get sunrise and sunset
            SunTimes sunrise = await DataRepository.GetSunTimesAsync("EHBK");

            // Set data to labels
            lblSunrise.Text = sunrise.Sunrise.ToString("G");
            lblSunset.Text = sunrise.Sunset.ToString("G");

        }

        private void ToolbarItem_Clicked(object sender, EventArgs e)
        {

        }

    }

    public class CustomSearchHandler : SearchHandler
    {
        // When user press enter and confirm get the icao code and search for the weather
        protected override void OnQueryConfirmed()
        {
            // Get the icao code
            string icao = Query;

            // Call wheather object
            Weather weather = new Weather();

            // Call frontend
            weather.frontend(icao);

        }
    }

}

Exaple

enter image description here

CodePudding user response:

this is creating a new instance of Weather and calling its frontend method. That won't do anything useful.

Weather weather = new Weather();
weather.frontend(icao);

Instead you need to use the existing instance that is already displayed to the user

there are many ways to do this, but this might be the simplest

// get the current page
var page = App.Current.MainPage;
// cast it to the correct type
var weather = (Weather)page;
// call its frontend method
page.frontend(icao);
  • Related