Waqas Idrees
Waqas Idrees

Reputation: 1471

How to calculate App Idle Time in MVVMCross Xamarin.iOS?

I am developing iOS Universal Application in Xamarin.iOS using MVVMCross. I want to calculate App Idle time I found the following useful help

iOS:Convert ObjC code to C#, How to know the time app has been idle

But there is an issue when i try to use this with MVVMCross which is that AppDelegate.cs in MvvmCross is inherited from MvxApplicationDelegate.cs

I am unable to override the following event in AppDelegate because it is not overriding UIApplication

public override void SendEvent (UIEvent uievent)
    {
        base.SendEvent (uievent);
        var allTouches = uievent.AllTouches;
        if (allTouches.Count > 0) {
            var phase = ((UITouch)allTouches.AnyObject).Phase;
            if (phase == UITouchPhase.Began || phase == UITouchPhase.Ended)
                ResetIdleTimer ();
        }
    }

Upvotes: 2

Views: 644

Answers (1)

Sven-Michael Stübe
Sven-Michael Stübe

Reputation: 14750

You were close to the answer.

In a default MvvMCross project, there is a Main.cs, that contains a Application class.

You just have to replace the null in following line with your UIApplication's child class name

UIApplication.Main(args, null, "AppDelegate");

e.g If your class name is MyApplication which is inherited from UIApplication then it should be like

UIApplication.Main(args, "MyApplication", "AppDelegate");

and add a class MyApplication

[Register("MyApplication")]
public class MyApplication : UIApplication
{
    public override void SendEvent(UIEvent uievent)
    {
        base.SendEvent(uievent);
        var allTouches = uievent.AllTouches;
        if (allTouches.Count > 0)
        {
            var phase = ((UITouch)allTouches.AnyObject).Phase;
            if (phase == UITouchPhase.Began || phase == UITouchPhase.Ended)
                ResetIdleTimer();
        }
    }
}

Upvotes: 2

Related Questions