简体   繁体   English

如何停止计时器/更改系统UI OnSleep Xamarin Android

[英]How to stop a timer/change system UI OnSleep Xamarin Android

So I have an app that on a button press: starts a timer, cycles through one (++) piece of data and hides the start button and instead shows a stop and next button. 因此,我有一个按一下按钮的应用程序:启动计时器,循环显示一个(++)数据,并隐藏开始按钮,而是显示停止按钮和下一个按钮。 I have looked into messaging center and I thought it was fixing the problem (here's the link Xamarin.Forms how do I access a public function from another CS file? ) but it didn't fix the problem completely. 我已经查看了消息传递中心,并认为它可以解决问题(这里是链接Xamarin.Forms,如何从另一个CS文件访问公共功能? ),但是并不能完全解决问题。

If the app has the timer running (aka you hit the start button) and then interrupt the process by hitting the home button on your phone, the app works fine and the app hides the stop/next buttons and shows the start button and stops the timer. 如果应用程序正在运行计时器(又是您按下“开始”按钮),然后通过按手机上的“主页”按钮来中断该过程,则该应用程序运行良好,并且该应用隐藏了“停止/下一步”按钮并显示“开始”按钮并停止计时器。 If you haven't started the process at all (aka you haven't hit the start button) and you hit the home button on your phone the app throws an exception error because what I'm changing with messaging center "doesn't need changing because it never changed". 如果您根本没有启动该过程(也没有按下开始按钮),并且您按下了手机上的主页按钮,则该应用程序将引发异常错误,因为我正在使用消息传递中心进行的更改“不需要改变,因为它从未改变”。 Is there a better way to handle this situation? 有没有更好的方法来处理这种情况?

Can I use if/else statements in app state with messagingcenter?? 我可以在Messenger中心使用处于应用状态的if / else语句吗? I'm stuck. 我被卡住了。

App.xaml.cs App.xaml.cs

protected override void OnSleep()
    {
        // Handle when your app sleeps
        Debug.WriteLine("~~~~~~~~~~~~~~OnSleep~~~~~~~~~~~~~");
        MessagingCenter.Send<App>(this, "OnSleep");
    }

MainPage.xaml.cs MainPage.xaml.cs中

MessagingCenter.Subscribe<App>(this, "OnSleep", (sender) => {
            //shows start button instead of stop button
            StartGrid.IsVisible = true;
            //hides stop button
            StopNextGrid.IsVisible = false;

            //stops timer
            timer.Stop();
            timer = null;
            //stops sound
            startSound.Stop();
            stopSound.Play();
        });

Just can see the partial code,you should check if your timer is initialized before executing the method . 只能看到部分代码, 执行该方法之前检查计时器是否已初始化

When you do not click the start button, you need to check whether the timer is initialized, in order to perform the following timer operation. 如果不单击开始按钮,则需要检查timer是否已初始化,以便执行以下计时器操作。

If no want to know whether timer is initialized. 如果不想知道计时器是否已初始化。 You can try this: 你可以试试看:

Modify in your notification handling method.If the state of your timer and button has not changed, you don't need to do anything in the notification.Here I use the timer as a judgment. 在您的通知处理方法中进行修改。如果您的计时器和按钮的状态未更改,则您无需在通知中做任何事情。在这里,我使用timer作为判断。

MessagingCenter.Subscribe<App>(this, "OnSleep", (sender) => {
//shows start button instead of stop button
if (null != timer)
{
    StartGrid.IsVisible = true;
    //hides stop button
    StopNextGrid.IsVisible = false;
    //stops timer
    timer.Stop();
    timer = null;
    //stops sound
    startSound.Stop();
    stopSound.Play();
}
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM