简体   繁体   English

C# - 定期刷新方法

[英]C# - Refresh method periodically

I want to refresh my UWP-UI periodically after like 5 minutes.我想在大约 5 分钟后定期刷新我的 UWP-UI。 I have a Method "Page_Loaded" where all information from the classes is sent to the UI-Elements.我有一个方法“Page_Loaded”,其中来自类的所有信息都发送到 UI 元素。 So if i refresh this method, the UI would do too, right?因此,如果我刷新此方法,UI 也会这样做,对吗?

The Code is like this:代码是这样的:

private async void Page_Loaded(object sender, RoutedEventArgs e)
        {
            RootObject myWeather = await Openweathermap.GetWeather();
            string icon = String.Format("http://openweathermap.org/img/wn/{0}@2x.png", myWeather.weather[0].icon);
            ResultImage.Source = new BitmapImage(new Uri(icon, UriKind.Absolute));

            TempTextBlock.Text = ((int)myWeather.main.temp).ToString() + "°";
            DescriptionTextBlock.Text = myWeather.weather[0].description;
            LocationTextBlock.Text = myWeather.name;

            var articlesList = NewsAPI.GetNews().Result.articles;
            lvNews.ItemsSource = articlesList;

            Welcometxt.Text = MainPage.WelcomeText();
        }

So, how do I refresh this method after 5 Minutes, so that it gets the new information and sends it to the UI?那么,如何在 5 分钟后刷新此方法,以便它获取新信息并将其发送到 UI?

So, how do I refresh this method after 5 Minutes, so that it gets the new information and sends it to the UI?那么,如何在 5 分钟后刷新此方法,以便它获取新信息并将其发送到 UI?

Repeatedly calling the Page_Loaded method is not a recommended practice, the recommended approach is to use DispatcherTimer , a timer within the UI thread.重复调用Page_Loaded方法不是推荐的做法,推荐的方法是使用 UI 线程内的计时器DispatcherTimer

We can extract the code inside Page_Loaded as a function.我们可以将Page_Loaded的代码提取为一个函数。

private DispatcherTimer _timer;
public MainPage()
{
    this.InitializeComponent();
    _timer = new DispatcherTimer();
    _timer.Interval = TimeSpan.FromMinutes(5);
    _timer.Tick += Timer_Tick;
}

private async Task GetData()
{
    RootObject myWeather = await Openweathermap.GetWeather();
    string icon = String.Format("http://openweathermap.org/img/wn/{0}@2x.png", myWeather.weather[0].icon);
    ResultImage.Source = new BitmapImage(new Uri(icon, UriKind.Absolute));

    TempTextBlock.Text = ((int)myWeather.main.temp).ToString() + "°";
    DescriptionTextBlock.Text = myWeather.weather[0].description;
    LocationTextBlock.Text = myWeather.name;

    var articlesList = NewsAPI.GetNews().Result.articles;
    lvNews.ItemsSource = articlesList;

    Welcometxt.Text = MainPage.WelcomeText();
}

private async void Timer_Tick(object sender, object e)
{
    await GetData();
}

private async void Page_Loaded(object sender, RoutedEventArgs e)
{
    await GetData();
    _timer.Start();
}

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    _timer.Stop();
    base.OnNavigatedFrom(e);
}

With DispatcherTimer.Tick , we can execute tasks regularly, and when we leave the page, we can stop the timer.使用DispatcherTimer.Tick ,我们可以定期执行任务,离开页面时,我们可以停止计时器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM