c# - 在 OnAppearing 函数上添加延迟
问题描述
我想在检查我的设备的互联网连接之前添加一个延迟。我有一个函数 OnAppearing()。当页面仅在 X 秒后出现时,我的应用程序将检查设备的互联网连接。
我有 2 个问题:
1. 如何添加延迟?
2.如果我添加延迟,我的页面会在延迟开始之前先出现还是我的页面在延迟之后才会出现?
protected async override void OnAppearing()
{
base.OnAppearing();
//Add delay before executing the code below
if (CrossConnectivity.Current.IsConnected){
//Some code here
}
}
解决方案
使用一劳永逸的任务,您的页面将继续加载/显示。
protected async override void OnAppearing()
{
base.OnAppearing();
Task.Run(async () =>
{
await Task.Delay(TimeSpan.FromSeconds(10));
if (CrossConnectivity.Current.IsConnected)
{
//Some code here
Device.BeginInvokeOnMainThread(() =>
{
// If you need to update an UI element
});
}
});
}
推荐阅读
- php - 如何使用 PHP 从表单在同一项目中发送 PUT 请求
- javascript - this.value 和 $(this).val() 有什么区别?
- mysql - 如何创建存储过程并在预定时间运行它(在 MYSQL 中)
- javascript - 为什么 Edge 中的 sessionStorage 与 Firefox/Chrome 不同,我该如何更改?
- python - 字典中字符串值的总和
- c - redis中run_with_period c宏背后的确切语法或规范是什么
- javascript - 比较日期时间与当前日期时间
- javascript - 将字符串转换为标准矩格式
- android - 用户不选择图像时出现错误如何解决
- android - 在 Main Activity 被销毁后,执行耗时任务的 Android Executor 实例是否仍然保持活动状态?