Created
December 10, 2014 10:36
-
-
Save aliozgur/179a98e6c7a548758c3c to your computer and use it in GitHub Desktop.
Xamarin.Forms : Get Notified for device orientation changes with 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; | |
public override void DidChangeStatusBarOrientation(UIApplication application, UIInterfaceOrientation oldStatusBarOrientation) | |
{ | |
var currentOrientation = UIApplication.SharedApplication.StatusBarOrientation; | |
bool isPortrait = currentOrientation == UIInterfaceOrientation.Portrait || currentOrientation == UIInterfaceOrientation.PortraitUpsideDown; | |
SendOrientationMessage(!isPortrait); | |
} | |
static void SendOrientationMessage(bool isLandscape) | |
{ | |
var msg = new DeviceOrientationChangeMessage() | |
{ | |
Orientation = isLandscape ? DeviceOrientation.Landscape : DeviceOrientation.Portrait | |
}; | |
MessagingCenter.Send<DeviceOrientationChangeMessage>(msg, DeviceOrientationChangeMessage.MessageId); | |
} | |
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; | |
} | |
} | |
} | |
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 enum DeviceOrientation | |
{ | |
Landscape, | |
Portrait | |
} | |
} | |
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 DeviceOrientationChangeMessage | |
{ | |
public DeviceOrientation Orientation | |
{ | |
get; | |
set; | |
} | |
public static string MessageId | |
{ | |
get | |
{ | |
return "OrientationChangeMessage"; | |
} | |
} | |
} | |
} | |
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.Linq; | |
using Android.App; | |
using Android.Content; | |
using Android.Content.PM; | |
using Android.Runtime; | |
using Android.Views; | |
using Android.Widget; | |
using Android.OS; | |
using Xamarin.Forms.Platform.Android; | |
using System.Net; | |
using Xamarin.Forms; | |
using Android.Content.Res; | |
namespace MyNamespace.Android | |
{ | |
[Activity (Label = "MyApp", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] | |
public class MainActivity : AndroidActivity | |
{ | |
protected override void OnCreate (Bundle bundle) | |
{ | |
base.OnCreate (bundle); | |
Xamarin.Forms.Forms.Init (this, bundle); | |
SetPage (BuildView()); | |
} | |
public override void OnConfigurationChanged (Configuration newConfig) | |
{ | |
base.OnConfigurationChanged (newConfig); | |
bool isLandscape = newConfig.Orientation == global::Android.Content.Res.Orientation.Landscape; | |
RootPage.Current.IsLandscape = isLandscape; | |
SendOrientationMessage(isLandscape); | |
} | |
static void SendOrientationMessage(bool isLandscape) | |
{ | |
var msg = new DeviceOrientationChangeMessage() { | |
Orientation = isLandscape ? DeviceOrientation.Landscape : DeviceOrientation.Portrait | |
}; | |
MessagingCenter.Send<DeviceOrientationChangeMessage>(msg, DeviceOrientationChangeMessage.MessageId); | |
} | |
static Page BuildView() | |
{ | |
return new RootPage(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment