如何在MVVMCross Xamarin.iOS中计算App空闲时间?

时间:2016-03-10 07:41:45

标签: c# xamarin mvvmcross

我正在使用MVVMCross在Xamarin.iOS中开发iOS Universal Application。我想计算App Idle时间我找到了以下有用的帮助

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

但是当我尝试将其与MVVMCross一起使用时会出现问题 MvvmCross中的AppDelegate.cs继承自MvxApplicationDelegate.cs

我无法在AppDelegate中覆盖以下事件,因为它没有覆盖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 ();
        }
    }

1 个答案:

答案 0 :(得分:2)

你接近答案了。

在默认的MvvMCross项目中,有一个 Main.cs ,其中包含Application类。

您只需将以下行中的null替换为UIApplication的子类名

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

例如,如果您的类名是继承自UIApplication的MyApplication,那么它应该是

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

并添加一个班级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();
        }
    }
}