本文介绍了iOS:将 ObjC 代码转换为 C#,如何知道 app 已经空闲的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 iOS 开发的新手,我正在使用 monotouch 来开发 iOS 应用程序,我想知道应用程序空闲的时间,我得到了 ObjC 代码,但无法将其转换为 c#.代码如下:

I am new to iOS development, I am using monotouch for developing the iOS apps, i want to know the time since the app was idle, i got the ObjC code but am unable to convert it into c#.Here is the code:

- (void)sendEvent:(UIEvent *)event {
    [super sendEvent:event];

    // Only want to reset the timer on a Began touch or an Ended touch, to reduce the number of timer resets.
    NSSet *allTouches = [event allTouches];
    if ([allTouches count] > 0) {
        // allTouches count only ever seems to be 1, so anyObject works here.
        UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
        if (phase == UITouchPhaseBegan || phase == UITouchPhaseEnded)
            [self resetIdleTimer];
    }
}

- (void)resetIdleTimer {
    if (idleTimer) {
        [idleTimer invalidate];
        [idleTimer release];
    }

    idleTimer = [[NSTimer scheduledTimerWithTimeInterval:maxIdleTime target:self selector:@selector(idleTimerExceeded) userInfo:nil repeats:NO] retain];
}

 - (void)idleTimerExceeded {
    NSLog(@"idle time exceeded");
}

谁能帮我把它转换成c#.

Can anyone help me in converting this to c#.

推荐答案

当然有更好的方法来实现这一点,但让我们将此 Obj-C 代码几乎逐行转换为 Xamarin.iOS C#:

There's certainly better way to achieve this, but let's do a almost line to line translation fo this Obj-C code to Xamarin.iOS C#:

sendEventUIApplication 的一个方法.对它进行子类化是非常罕见的,请参阅

sendEvent is a method of UIApplication. It's very uncommon to subclass it, see Subclassing notes on UIApplication class reference

一旦你对它进行了子类化,你必须指示运行时使用它,这在 Main 方法中完成,通常在 Main.cs 中找到.这是修改后的 Main.cs 现在的样子.

Once you subclassed it, you have to instruct the runtime to use it, that's done in the Main method, normally found in Main.cs. Here's what the modified Main.cs looks like now.

public class Application
{
    // This is the main entry point of the application.
    static void Main (string[] args)
    {
        UIApplication.Main (args, "MyApplication", "AppDelegate");
    }
}

[Register ("MyApplication")]
public class MyApplication : UIApplication
{

}

注意类上的 Register 属性,用作 UIApplication.Main 的第二个参数.

Notice the Register attribute on the class, used as second argument of UIApplication.Main.

现在,让我们真正翻译您的代码:

Now, let's translate your code for real:

[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 ();
        }
    }

    NSTimer idleTimer;
    void ResetIdleTimer ()
    {
        if (idleTimer != null) {
            idleTimer.Invalidate ();
            idleTimer.Release ();
        }

        idleTimer = NSTimer.CreateScheduledTimer (TimeSpan.FromHours (1), TimerExceeded);
    }

    void TimerExceeded ()
    {
        Debug.WriteLine ("idle time exceeded");
    }
}

我用 TimeSpan.FromHours (1) 替换了 maxIdleTime.否则,您将拥有与 Obj-C 相同的行为,包括错误(如果有的话)(不过看起来没问题).

I replaced maxIdleTime by TimeSpan.FromHours (1). Otherwise, you'll have the same behaviour as the Obj-C one, including bugs if any (it looks ok though).

这篇关于iOS:将 ObjC 代码转换为 C#,如何知道 app 已经空闲的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 09:01