c# - 每隔几秒自动刷新或更新内容页面

标签 c# xamarin xamarin.forms

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

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

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

最佳答案

Xamarin.Forms 有一个用于启动计时器的 API,您可能会发现它对此很有用,已记录 here .

Device.StartTimer (TimeSpan.FromSeconds(10), () => {
    // If you want to update UI, make sure its on the on the main thread.
    // Otherwise, you can remove the BeginInvokeOnMainThread
    Device.BeginInvokeOnMainThread(() => methodRunPeriodically());
    return shouldRunAgain;
});

关于c# - 每隔几秒自动刷新或更新内容页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41574642/

相关文章:

c# - 是否可以创建一个控制台窗口(由 AllocConsole 创建)作为 GUI 子窗口?如果是这样,如何?

c# - 是否可以在不创建 ViewModel 对象的情况下从 .NET 中的 REST api 获取复杂的 Entity Framework 对象?

xamarin.ios - xamarin VisualStudio for ios 中没有附加设备

xamarin - 如何将 ReactiveUI 的 RoutedViewHost 与 Xamarin Forms Shell 一起使用?

c# - Android构建错误 "failed to create JavaTypeInfo for class":Xamarin

xamarin - 使用 Xamarin Forms 在每个页面上显示不同的工具栏按钮

c# - 如何控制XML中的命名空间前缀?

c# - 锁定 ASP.NET 应用程序变量

sql-server - 从azure函数访问azure数据库

c# - 是否可以触发 OnPropertyChanged 事件,而无需在 ViewModel 上的属性 setter 中显式调用它?