每隔X个时间运行后台任务

Mau*_*ury 3 xamarin xamarin.forms

我想开始一项服务,在所有平台上偶尔检查一下是否有通知出现.是否有任何nuget连接所有平台或一些例子?

pna*_*avk 9

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

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;
});
Run Code Online (Sandbox Code Playgroud)

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

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