Skip to main content
 首页 » 编程设计

c#之NSTimer之适用于 iOS 10 但不适用于 iOS 9.3

2024年01月31日67三少

我在 Xamarin 开发的 iOS 应用程序中使用 NSTimer。应用程序在 iOS 10 (iPhone 5S) 上运行良好,但在装有 iOS 9.3.5 的 iPad 上崩溃。
为什么会发生这种情况,我该如何解决?

代码如下:

timer = NSTimer.CreateScheduledTimer(0.3, true, delegate 
{ 
    List<String> keys = this.beaconRegions.Keys.ToList(); 
    foreach (string identifier in keys) 
    { 
        this.locationManager.StopRangingBeacons((CLBeaconRegion)this.beaconRegions[identifier]); 
        this.locationManager.StopMonitoring(this.beaconRegions[identifier]); 
        this.locationManager.StartMonitoring(this.beaconRegions[identifier]); 
        this.locationManager.StartRangingBeacons(this.beaconRegions[identifier]); 
    } 
 
}); 

这里是崩溃日志的详细信息:

4   CoreFoundation                  0x21c9a234 0x21c72000 + 164404 
 
5   iBeacon                         0x0023564c wrapper_managed_to_native_ObjCRuntime_Messaging_objc_msgSend_intptr_intptr_double_bool_intptr (/<unknown>:1) 
 
6   iBeacon                         0x001e8ecc Foundation_NSTimer_CreateScheduledTimer_double_bool_System_Action_1_Foundation_NSTimer (NSTimer.g.cs:134) 
 
7   iBeacon                         0x001de5b4 iBeacon_LocationManager_turnOnMonitoring (LocationManager.cs:277) 
 
8   iBeacon                         0x001de2ac iBeacon_LocationManager_startMonitoringForBeacons (LocationManager.cs:250) 

LocationManager.cs:277 -- 这是我为 NStimer 编写代码的代码行。

请您参考如下方法:

如果你在iOS项目中,你可以直接使用System.Threading.Timer。 如果你想在PCL工程中使用定时器,那么你可以引用这个链接:https://forums.xamarin.com/discussion/17227/timer-in-portable-class-library