- 自由旋轉 : 表示使用者可以旋轉螢幕,使用直式或者橫式螢幕的方式來操作
- 限制直式 : 應用程式將會切換成為直式模式來操作,而且不能夠透過旋轉螢幕的方式,切換成為橫式模式。
- 限制橫式 : 應用程式將會切換成為橫式模式來操作,而且不能夠透過旋轉螢幕的方式,切換成為直式模式。
<StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
<Button Text="自由旋轉"
Command="{Binding OrientationCommand}" CommandParameter="自由旋轉"/>
<Button Text="限制直式"
Command="{Binding OrientationCommand}" CommandParameter="限制直式"/>
<Button Text="限制橫式"
Command="{Binding OrientationCommand}" CommandParameter="限制橫式"/>
</StackLayout>
public ScreenOrientationPageViewModel(INavigationService navigationService,
IEventAggregator eventAggregator)
{
this.navigationService = navigationService;
this.eventAggregator = eventAggregator;
OrientationCommand = new DelegateCommand<string>(x =>
{
CustomScreenOrientationPayload customScreenOrientationPayload = new CustomScreenOrientationPayload();
if (x == "自由旋轉")
{
customScreenOrientationPayload.CustomScreenOrientation = CustomScreenOrientation.Unspecified;
}
else if (x == "限制直式")
{
customScreenOrientationPayload.CustomScreenOrientation = CustomScreenOrientation.UserPortrait;
}
else
{
customScreenOrientationPayload.CustomScreenOrientation = CustomScreenOrientation.UserLandscape;
}
eventAggregator.GetEvent<CustomScreenOrientationEvent>().Publish(customScreenOrientationPayload);
});
}
public enum CustomScreenOrientation
{
UserPortrait,
UserLandscape,
Unspecified
}
public class CustomScreenOrientationEvent : PubSubEvent<CustomScreenOrientationPayload>
{
}
public class CustomScreenOrientationPayload
{
public CustomScreenOrientation CustomScreenOrientation { get; set; }
}
如何指定該應用程式預設的螢幕轉向設定
[Activity(Label = "ForceOrientation", Icon = "@mipmap/ic_launcher",
Theme = "@style/MainTheme", MainLauncher = true,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
ScreenOrientation = ScreenOrientation.Portrait)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App(new AndroidInitializer()));
}
}
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
由程式來控制螢幕要顯示的方向,到底是直向還是橫向
[Activity(Label = "ForceOrientation", Icon = "@mipmap/ic_launcher",
Theme = "@style/MainTheme", MainLauncher = true,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
ScreenOrientation = ScreenOrientation.Portrait)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App(new AndroidInitializer()));
SubscribePrismEvent();
}
public void SubscribePrismEvent()
{
IContainerProvider containerProvider = App.Current.Container;
IEventAggregator eventAggregator = containerProvider.Resolve<IEventAggregator>();
eventAggregator.GetEvent<CustomScreenOrientationEvent>().Subscribe(x =>
{
if (x.CustomScreenOrientation == CustomScreenOrientation.Unspecified)
{
RequestedOrientation = ScreenOrientation.Unspecified;
}
else if (x.CustomScreenOrientation == CustomScreenOrientation.UserPortrait)
{
RequestedOrientation = ScreenOrientation.Portrait;
}
else
{
RequestedOrientation = ScreenOrientation.Landscape;
}
});
}
}
// The UIApplicationDelegate for the application. This class is responsible for launching the
// User Interface of the application, as well as listening (and optionally responding) to
// application events from iOS.
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
CustomScreenOrientation ScreenOrientation = CustomScreenOrientation.UserPortrait;
//
// This method is invoked when the application has loaded and is ready to run. In this
// method you should instantiate the window, load the UI into it and then make the window
// visible.
//
// You have 17 seconds to return from this method, or iOS will terminate your application.
//
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App(new iOSInitializer()));
SubscribePrismEvent();
return base.FinishedLaunching(app, options);
}
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, [Transient] UIWindow forWindow)
{
if (ScreenOrientation == CustomScreenOrientation.Unspecified)
{
return UIInterfaceOrientationMask.All;
}
else if (ScreenOrientation == CustomScreenOrientation.UserLandscape)
{
return UIInterfaceOrientationMask.Landscape;
}
else
{
return UIInterfaceOrientationMask.Portrait;
}
}
public void SubscribePrismEvent()
{
IContainerProvider containerProvider = App.Current.Container;
IEventAggregator eventAggregator = containerProvider.Resolve<IEventAggregator>();
eventAggregator.GetEvent<CustomScreenOrientationEvent>().Subscribe(x =>
{
ScreenOrientation = x.CustomScreenOrientation;
NSNumber n ;
NSString key = new NSString("orientation");
if (x.CustomScreenOrientation == CustomScreenOrientation.Unspecified)
{
n = new NSNumber((int)UIInterfaceOrientation.Unknown);
}
else if (x.CustomScreenOrientation == CustomScreenOrientation.UserPortrait)
{
n = new NSNumber((int)UIInterfaceOrientation.Portrait);
}
else
{
n = new NSNumber((int)UIInterfaceOrientation.LandscapeLeft);
}
UIDevice.CurrentDevice.SetValueForKey(
new NSNumber((int)n),
new NSString("orientation"));
UIViewController.AttemptRotationToDeviceOrientation();
});
}
}