通过searchHandler Xamarin C#使用来自APi的新数据刷新页面

voase2hg  于 2022-12-07  发布在  C#
关注(0)|答案(1)|浏览(171)

我有一个关于xamarin中的函数search Handler的问题。我刚刚开始学习xamarin,我正在尝试在我的项目中使用xamarin中的search handler来刷新我的页面,使用来自API的新数据。目前,我已经很幸运地检索到了数据,但当我这样做时,它会创建一个新页面,可以这么说,但这不是我想要的。他会用新的数据重新加载页面。我也已经试着用“Shell.Current.Navigation.PopAsync();“但是没有任何残留的结果,谁知道我怎么才能达到我的目的?另外,我也想消除你在搜索之后的模糊,提前谢谢你了!”

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);

        }
    }

}

0yg35tkg

0yg35tkg1#

这是创建Weather的一个新示例并调用它的frontend方法。这不会做任何有用的事情。

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

相反,您需要使用已向用户显示的现有示例
有许多方法可以做到这一点,但这可能是最简单的方法

// 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);

相关问题