[英]How to set ContentPage orientation or screen orientation on particular page in Xamarin.Forms cross platform
How to set Content-Page orientation or screen orientation on particular page in Xamarin.Forms
cross platform. 如何在Xamarin.Forms
跨平台的特定页面上设置内容页面方向或屏幕方向。
I have set ScreenOrientation = ScreenOrientation
. 我已经设置了ScreenOrientation = ScreenOrientation
。
Portrait at property of all platform but I want to show some page show in both variation means Portrait and Landscape, so how to set on page in `xamarin.forms. 在所有平台属性的肖像,但我想在两个变化中显示一些页面显示纵向和横向,所以如何在`xamarin.forms中设置页面。
set screen orientation not device wise but set on page-wise some pages show in Landscape & some page show in Portrait in xamarin forms cross- platform 设置屏幕方向不是设备明智但在页面上设置一些页面显示在横向和一些页面显示在肖像中的xamarin形式跨平台
You can do this by creating a dependency using DependencyService
for specific platforms. 您可以通过使用DependencyService
为特定平台创建依赖关系来完成此操作。
Try doing this: 试着这样做:
Interface 接口
public interface IOrientationService
{
void Landscape();
void Portrait();
}
For Android: 对于Android:
[assembly: Xamarin.Forms.Dependency(typeof(OrientationService))]
namespace Orientation
{
public class OrientationService: IOrientationService
{
public void Landscape()
{
((Activity)Forms.Context).RequestedOrientation = ScreenOrientation.Landscape;
}
public void portrait()
{
((Activity)Forms.Context).RequestedOrientation = ScreenOrientation.Portrait;
}
}
}
For iOS: 对于iOS:
[assembly: Xamarin.Forms.Dependency(typeof(OrientationService))]
namespace Orientation
{
public class OrientationService: IOrientationService
{
public void Landscape()
{
UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeLeft), new NSString("orientation"));
}
public void Portrait()
{
UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation"));
}
}
}
Then you can use it like this 然后你可以像这样使用它
DependencyService.Get<IOrientationService>().Landscape();
DependencyService.Get<IOrientationService>().Portrait();
Hope it helps! 希望能帮助到你!
So this is how I do it in Android. 这就是我在Android中的表现。 You have to add the code to MainActivity.cs
您必须将代码添加到MainActivity.cs
[Activity(...., ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
or your app will throw an exception if the user turns his device. 或者,如果用户转动设备,您的应用会抛出异常。
Then add this: 然后添加:
//To check if device is allowed to rotate
private bool _allowLandscape;
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
private bool _allowLandscape;
protected override void OnCreate(Bundle bundle)
{
switch (Device.Idiom)
{
case TargetIdiom.Phone:
RequestedOrientation = ScreenOrientation.Portrait;
break;
case TargetIdiom.Tablet:
RequestedOrientation = ScreenOrientation.Landscape;
break;
}
base.OnCreate(bundle);
//Setup additional stuff that you need
//Calls from the view that should rotate
MessagingCenter.Subscribe<GraphicsView>(this, "graphic", sender =>
{
_allowLandscape = true;
OnConfigurationChanged(new Configuration());
});
//When the page is closed this is called
MessagingCenter.Subscribe<GraphicsView>(this, "return", sender =>
{
_allowLandscape = false;
OnConfigurationChanged(new Configuration());
});
LoadApplication(new App());
}
public override void OnConfigurationChanged(Configuration newConfig)
{
base.OnConfigurationChanged(newConfig);
switch (newConfig.Orientation)
{
case Orientation.Landscape:
switch (Device.Idiom)
{
case TargetIdiom.Phone:
if (!_allowLandscape)
{
LockRotation(Orientation.Portrait);
}
break;
case TargetIdiom.Tablet:
LockRotation(Orientation.Landscape);
break;
}
break;
case Orientation.Portrait:
switch (Device.Idiom)
{
case TargetIdiom.Phone:
if (!_allowLandscape)
{
LockRotation(Orientation.Portrait);
}
break;
case TargetIdiom.Tablet:
LockRotation(Orientation.Landscape);
break;
}
break;
case Orientation.Undefined:
if (Device.Idiom == TargetIdiom.Phone && _allowLandscape)
{
LockRotation(Orientation.Landscape);
}
else if (Device.Idiom == TargetIdiom.Phone && !_allowLandscape)
{
LockRotation(Orientation.Portrait);
}
break;
}
}
private void LockRotation(Orientation orientation)
{
switch (orientation)
{
case Orientation.Portrait:
RequestedOrientation = ScreenOrientation.Portrait;
break;
case Orientation.Landscape:
RequestedOrientation = ScreenOrientation.Landscape;
break;
}
}
}
Hope this will work for you. 希望这对你有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.