如何在Xamarin.phs跨平台上设置ContentPage方向或屏幕方向
问题描述:
如何在Xamarin.Forms
跨平台的特定页面上设置内容页面方向或屏幕方向。如何在Xamarin.phs跨平台上设置ContentPage方向或屏幕方向
我已设置ScreenOrientation = ScreenOrientation
。
肖像属性的所有平台,但我想显示一些页面显示在两个变化意味着肖像和风景,所以如何在`xamarin.forms页面上设置。
设置屏幕方向而不是设备明智的,但在设置页面明智的某些页面显示景观&一些页面纵向显示在xamarin形式的跨平台
答
这就是我如何做到这一点在Android中。您必须将代码添加到MainActivity.cs
[Activity(...., ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
或您的应用程序,如果用户打开他的设备会抛出异常。
然后补充一点:
//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;
}
}
}
希望这会为你工作。
答
您可以通过为特定平台使用DependencyService
创建依赖项来实现此目的。
试着这样做:
接口
public interface IOrientationService
{
void Landscape();
void Portrait();
}
对于安卓
[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;
}
}
}
对于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"));
}
}
}
然后你可以使用它像这样
DependencyService.Get<IOrientationService>().Landscape();
DependencyService.Get<IOrientationService>().Portrait();
希望它有帮助!
设置屏幕方向不是设备明智的,但在页面上设置一些页面显示在横向和一些页面显示肖像在Xamarin形式跨平台 – sonu