Created
December 10, 2014 10:46
-
-
Save aliozgur/9322251ebb9b31fb412b to your computer and use it in GitHub Desktop.
Xamarin.Forms : Using NSTimer and MessagingCenter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using MonoTouch.Foundation; | |
using MonoTouch.UIKit; | |
using Xamarin.Forms; | |
using System.Net; | |
using MyNamespace.Models; | |
namespace MyNamespace.iOS | |
{ | |
[Register("AppDelegate")] | |
public partial class AppDelegate : UIApplicationDelegate | |
{ | |
UIWindow window; | |
RootPage _root; | |
NSTimer _realtimPowerChartTimer; | |
public override bool FinishedLaunching(UIApplication app, NSDictionary options) | |
{ | |
window = new UIWindow(UIScreen.MainScreen.Bounds); | |
Forms.Init(); | |
window.RootViewController = BuildView(); | |
window.MakeKeyAndVisible(); | |
return true; | |
} | |
private UINavigationController BuildView() | |
{ | |
_root = new RootPage(); | |
var controller = _root.CreateViewController(); | |
UINavigationController nav = new UINavigationController(controller); | |
nav.SetNavigationBarHidden(true, false); | |
return nav; | |
} | |
void StartTimers() | |
{ | |
StartRealtimePowerChartTimer(); | |
// More timers can be started here | |
} | |
void StopTimers() | |
{ | |
StopRealtimePowerChartTimer(); | |
// More timers can be stopped here | |
} | |
void StartRealtimePowerChartTimer() | |
{ | |
StopRealtimePowerChartTimer(); | |
int powerInterval = 15;//seconds | |
if (powerInterval > 0) | |
{ | |
_realtimPowerChartTimer = NSTimer.CreateRepeatingScheduledTimer(powerInterval, new NSAction(() => | |
{ | |
MessagingCenter.Send<MessagingCenterMessage<DateTime>>(new MessagingCenterMessage<DateTime>{ Data = DateTime.Now},"REALTIME_POWERCHART_TIMER_MESSAGE"); | |
})); | |
_realtimPowerChartTimer.Fire(); | |
} | |
} | |
void StopRealtimePowerChartTimer() | |
{ | |
if (_realtimPowerChartTimer != null) | |
{ | |
_realtimPowerChartTimer.Invalidate(); | |
_realtimPowerChartTimer.Dispose(); | |
_realtimPowerChartTimer = null; | |
} | |
} | |
public override void OnActivated(UIApplication application) | |
{ | |
StartTimers(); | |
} | |
public override void OnResignActivation(UIApplication application) | |
{ | |
StopTimers(); | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace MyNamespace | |
{ | |
public class MessagingCenterMessage<T> | |
{ | |
public MessagingCenterMessage() | |
{ | |
} | |
public T Data | |
{ | |
get; | |
set; | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MyNamespace | |
{ | |
public class MyPage:ContentPage | |
{ | |
protected override void OnAppearing() | |
{ | |
base.OnAppearing(); | |
MessagingCenter.Subscribe<MessagingCenterMessage<DateTime>>(this,"REALTIME_POWERCHART_TIMER_MESSAGE", (message) => | |
{ | |
//TODO: Do whatever you need to do here | |
}); | |
} | |
protected override void OnDisappearing() | |
{ | |
MessagingCenter.Unsubscribe<MessagingCenterMessage<DateTime>>(this, "REALTIME_POWERCHART_TIMER_MESSAGE"); | |
base.OnDisappearing(); | |
} | |
} | |
} |
Do not forget
- To stop your timers when your app is send to background,
- To restart the timers when your app is back in the foreground.
Please note, detection method and events of whether your app is active or in the background is platform specific
Ali Bey [email protected] adresinize mail yolladım. Rica etsem bakabilir misiniz ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use the same approach by utilizing System.Threading.Timer on Android