Xamarin.Ios BeingBackgroundTask参数
Xamarin.Ios BeingBackgroundTask parameters
我正在尝试 Xamarin 的这段代码。 Ios 后台任务文档:https://docs.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/ios-backgrounding-techniques/ios-backgrounding-with-tasks。它说 行 6 中的变量 taskId
有 CS0841: Cannot use local variable 'taskId' before it is declared
。我应该如何分配 taskId
?另外,如何声明myFlag
?我找不到有关如何使用此变量的任何说明。
Task.Factory.StartNew( () => {
//expirationHandler only called if background time allowed exceeded
var taskId = UIApplication.SharedApplication.BeginBackgroundTask(() => {
Console.WriteLine("Exhausted time");
UIApplication.SharedApplication.EndBackgroundTask(taskId);
});
while(myFlag == true)
{
Console.WriteLine(UIApplication.SharedApplication.BackgroundTimeRemaining);
myFlag = SomeCalculationNeedsMoreTime();
}
//Only called if loop terminated due to myFlag and not the expiration of time
UIApplication.SharedApplication.EndBackgroundTask(taskId);
});
这是正确的 link : iOS Backgrounding with Tasks .
How should I assign the taskId?
正如link中提到的,taskId
是注册过程中returns的唯一标识符,我们只需要在调用EndBackgroundTask
时传递它即可。
nint taskID = UIApplication.SharedApplication.BeginBackgroundTask( () => {});
UIApplication.SharedApplication.EndBackgroundTask(taskId);
how do you declare myFlag?
这个变量只是表示后台任务结束的标志,如果你不想停止任务就设置为true
。
我正在尝试 Xamarin 的这段代码。 Ios 后台任务文档:https://docs.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/ios-backgrounding-techniques/ios-backgrounding-with-tasks。它说 行 6 中的变量 taskId
有 CS0841: Cannot use local variable 'taskId' before it is declared
。我应该如何分配 taskId
?另外,如何声明myFlag
?我找不到有关如何使用此变量的任何说明。
Task.Factory.StartNew( () => {
//expirationHandler only called if background time allowed exceeded
var taskId = UIApplication.SharedApplication.BeginBackgroundTask(() => {
Console.WriteLine("Exhausted time");
UIApplication.SharedApplication.EndBackgroundTask(taskId);
});
while(myFlag == true)
{
Console.WriteLine(UIApplication.SharedApplication.BackgroundTimeRemaining);
myFlag = SomeCalculationNeedsMoreTime();
}
//Only called if loop terminated due to myFlag and not the expiration of time
UIApplication.SharedApplication.EndBackgroundTask(taskId);
});
这是正确的 link : iOS Backgrounding with Tasks .
How should I assign the taskId?
正如link中提到的,taskId
是注册过程中returns的唯一标识符,我们只需要在调用EndBackgroundTask
时传递它即可。
nint taskID = UIApplication.SharedApplication.BeginBackgroundTask( () => {});
UIApplication.SharedApplication.EndBackgroundTask(taskId);
how do you declare myFlag?
这个变量只是表示后台任务结束的标志,如果你不想停止任务就设置为true
。