Share via


Device.StartTimer(TimeSpan, Func<Boolean>) 方法

定義

使用裝置時鐘功能啟動週期性計時器。

public static void StartTimer (TimeSpan interval, Func<bool> callback);
static member StartTimer : TimeSpan * Func<bool> -> unit

參數

interval
System.TimeSpan

回呼引動過程的間隔。

callback
System.Func<System.Boolean>

計時器結束時所要執行的動作。

備註

當回呼傳 true回時,定時器會保留週期性。

如果您想要定時器內的程式代碼在UI線程上互動 (例如設定標籤的文字或顯示警示) ,它應該在表達式內完成,這會在定時器內 BeginInvokeOnMainThread 巢狀 (如下所示) 。

Device.StartTimer (new TimeSpan (0, 0, 60), () =>
{
    // do something every 60 seconds
    Device.BeginInvokeOnMainThread (() => 
    {
      // interact with UI elements
    });
    return true; // runs again, or false to stop
});

適用於