每隔几秒自动刷新或更新内容页面 [英] Refresh or update content page every few seconds automatically

查看:35
本文介绍了每隔几秒自动刷新或更新内容页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Xamarin.forms (PCL),我需要每隔几秒刷新/更新内容页面的数据.从 viewmodel 中的 API 检索数据.

I am using Xamarin.forms (PCL) and I need to refresh/update Content Page with its data every few seconds. The data is retrieved from API in the viewmodel.

是否有任何方法或处理程序可以定期用于在 page.xaml.cs 中定期调用 Get Api,例如:

Is there any method or handler that can be used periodically to call the Get Api periodically inside the page.xaml.cs, something like:

methodRunPeriodically()
{
 userdata = await UserService.GetUserasync(_UserViewModel.EmployeeId);
}

推荐答案

基于上述问题中的代码,您将确保:

Based on the code in the above question, you would ensure that:

  1. 您的 userdata 对象实现 IPropertyChange 如下:

  1. Your userdata object implements IPropertyChange as follows:

//Other usings skipped for brevity
...
...
using System.ComponentModel;
using System.Runtime.CompilerServices;

// This is a simple user class that 
// implements the IPropertyChange interface.
public class DemoUser : INotifyPropertyChanged
{
    // These fields hold the values for the public properties.
    private string userName = string.Empty;
    private string phoneNumber = string.Empty;

    public event PropertyChangedEventHandler PropertyChanged;

    // This method is called by the Set accessor of each property.
    // The CallerMemberName attribute that is applied to the optional propertyName
    // parameter causes the property name of the caller to be substituted as an argument.
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public DemoUser()
    {
    }
    public string Id { get; set; }
    public string UserName
    {
        get
        {
            return this.userName;
        }

        set
        {
            if (value != this.userName)
            {
                this.userName = value;
                NotifyPropertyChanged();
            }
        }
    }

    public string PhoneNumber
    {
        get
        {
            return this.phoneNumber;
        }

        set
        {
            if (value != this.phoneNumber)
            {
                this.phoneNumber = value;
                NotifyPropertyChanged();
            }
        }
    }
}

  • 然后在您的 ContentPage 中尝试以下操作(我对上面其他人的代码稍作修改):

  • In your ContentPage, you then try the following, (I slightly modified the code by others above):

    public class UserPage : ContentPage
    {
        private DemoUser demoUser;
        private int intervalInSeconds;
    
        public UserPage()
        {
            //Assuming this is a XAML Page....
            InitializeComponent();
        }
        public UserPage(DemoUser demoUser, int intervalInSeconds = 10) : this()
        {
            this.demoUser = demoUser;
            this.intervalInSeconds = intervalInSeconds;
    
            this.BindingContext = this.demoUser; 
    
            Device.StartTimer(TimeSpan.FromSeconds(this.intervalInSeconds), () =>
            {
                Device.BeginInvokeOnMainThread(() => refreshDemoUser());
                return true;
            });
        }
    
        private async void refreshDemoUser()
        {
            this.demoUser = await getDemoUserById(this.demoUser.Id);
        }
    }
    

  • 这篇关于每隔几秒自动刷新或更新内容页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆