这是代码:
- (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#.
解决方法@H_502_19@ 实现这一点肯定有更好的方法,但让我们对这个Obj-C代码进行几乎逐行转换为Xamarin.iOS C#:sendEvent是UIApplication的一种方法.将它子类化是非常罕见的,请参阅UIApplication class reference上的子类化注释
一旦将其子类化,就必须指示运行时使用它,这在Main方法中完成,通常在Main.cs.中找到.这是修改后的Main.cs现在的样子.
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的第二个参数.
现在,让我们将您的代码翻译为真实:
[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相同的行为,包括BUG(如果有的话)(虽然看起来不错).
总结以上是内存溢出为你收集整理的iOS:将ObjC代码转换为C#,如何知道应用程序空闲的时间全部内容,希望文章能够帮你解决iOS:将ObjC代码转换为C#,如何知道应用程序空闲的时间所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)