c# - 如何使计时器运行而不冻结页面
问题描述
我正在制作 xamarin 欢迎页面的斜线屏幕页面。
我想实现将SlashScreen
在 5 秒后关闭的计时器。并在 xaml 中显示秒数。
<Label TextColor="Black" FontSize= "20" Text="{Binding timerSecond}"/>
这是我的课:
public partial class SlashScreen : ContentPage
{
int timerSecond = 5;
public SlashScreen()
{
InitializeComponent();
}
protected override void OnAppearing()
{
var timer = new System.Timers.Timer(1000);
timer.Start();
timer.Elapsed += (obj, args) =>
{
if (timerSecond == 0)
{
timer.Stop();
Application.Current.MainPage = new MainPage();
}
else
{
timerSecond--;
}
};
}
}
谢谢你的帮助。
解决方案
这未经测试。但它应该适合你
public partial class SlashScreen : ContentPage
{
int _timerSecound = 5;
public SlashScreen()
{
InitializeComponent();
}
protected override void OnAppearing()
{
Device.StartTimer(TimeSpan.FromSeconds(1), () =>
{
_timerSecound --;
timeSecound.Text = _timerSecound.ToString();
if (_timerSecound <=0){
/// your code here,
// i dont know if you could create a new mainpage, this should not work.
Application.Current.MainPage = new MainPage();
// dont know if this will work for you, but this will close
// the current and go back too the prev screen
this.Navigation.PopAsync ();
this.Navigation.PopToRootAsync();
return false; // stop
}
return true; // repeat
};
}
}
推荐阅读
- python-3.x - 如何使用交易 ID 查找 PayPal 交易?
- angular - 如何处理 component.spec.ts Jasmine 中的订阅?
- ios - SwiftUI List 顶部和底部的不同背景颜色
- javascript - 带有 useContext 钩子的 useReducer 行为怪异,并且运行和无限循环
- c# - ListView 不显示 BindingCollection 的内容
- cassandra - Cassandra 警告:使用没有分区键的聚合查询
- javascript - 将 amazon cognito 添加到我的 react-native expo tsx 项目后出现问题
- c - C Graphics - 如何以特定角度移动对象
- material-ui - Material UI Tooltip 标题文字内容有一个打开对话框的按钮,打开后对话框消失
- github - Github Actions 中的 repository_dispatch 和 workflow_dispatch 有什么区别?