如何解决Xamarin表格块旋转-仅纵向视图
我需要阻止在特定页面上旋转,并且该功能必须同时适用于Android和iOS。
我尝试使用NuGet Plugin.DeviceOrientation并添加以下代码
protected override void OnAppearing()
{
base.OnAppearing();
if (CrossDeviceOrientation.IsSupported)
CrossDeviceOrientation.Current.LockOrientation(Plugin.DeviceOrientation.Abstractions.DeviceOrientations.Portrait);
}
protected override void OnDisappearing()
{
base.OnDisappearing();
if (CrossDeviceOrientation.IsSupported)
CrossDeviceOrientation.Current.UnlockOrientation();
}
但是抛出异常: System.NullReferenceException:'对象引用未设置为对象的实例。'
解决方法
对于Xamarin.Forms,您可以依赖服务。
创建界面:
public interface IRotate
{
void ForcePortrait();
}
Android:
[assembly: Xamarin.Forms.Dependency(typeof(RoateHandler))]
namespace RotatePage.Droid
{
class RoateHandler : IRotate
{
public void ForcePortrait()
{
((Activity)Forms.Context).RequestedOrientation = ScreenOrientation.Portrait;
}
}
}
iOS:
[assembly: Xamarin.Forms.Dependency(typeof(RoateImplementation))]
namespace RotatePage.iOS
{
class RoateImplementation : IRotate
{
public void ForcePortrait()
{
UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait),new NSString("orientation"));
}
}
}
用法:
DependencyService.Get()。ForcePortrait();
截屏:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。