且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

每 X 时间运行一次后台任务

更新时间:2022-12-26 13:19:10

您可以使用 Device.StartTimer(TimeSpan minutes) 方法启动后台任务,该任务将在给定的时间跨度后重复.这是一个代码示例:

You can use the Device.StartTimer(TimeSpan minutes) method to start a background task that will repeat after the given time span. Here is a code example:

var minutes = TimeSpan.FromMinutes (3); 

Device.StartTimer (minutes, () => {

    // call your method to check for notifications here

    // Returning true means you want to repeat this timer
    return true;
});

这包含在 Xamarin Forms 中,因此您不需要任何特定于平台的逻辑.

This is included with Xamarin Forms, so you don't need any platform specific logic.

http://iosapi.xamarin.com/index.aspx?link=M%3AXam​​arin.Forms.Device.StartTimer(System.TimeSpan%2CSystem.Func%7BSystem.Boolean%7D)