You can use Invoke platform code to achieve this.Platform code can be invoked from cross-platform code by using conditional compilation, or by using partial classes and partial methods.
Here I try to use partial classes and partial methods to achieve this(Based on the official sample, I added the test code ,which could help you understand this concept.).
Please refer to the following code:
public enum DeviceOrientation
{
Undefined,
Landscape,
Portrait
}
namespace InvokePlatformCodeDemos.Services.PartialMethods
{
public partial class DeviceOrientationService
{
public partial DeviceOrientation GetOrientation();
public partial void SetDeviceOrientation(DisplayOrientation deviceOrientation);
}
}
In Android
public partial class DeviceOrientationService
{
public partial DeviceOrientation GetOrientation()
{
IWindowManager windowManager = Android.App.Application.Context.GetSystemService(Context.WindowService).JavaCast<IWindowManager>();
SurfaceOrientation orientation = windowManager.DefaultDisplay.Rotation;
bool isLandscape = orientation == SurfaceOrientation.Rotation90 || orientation == SurfaceOrientation.Rotation270;
return isLandscape ? DeviceOrientation.Landscape : DeviceOrientation.Portrait;
}
private static readonly IReadOnlyDictionary<DisplayOrientation, ScreenOrientation> _androidDisplayOrientationMap =
new Dictionary<DisplayOrientation, ScreenOrientation>
{
[DisplayOrientation.Landscape] = ScreenOrientation.Landscape,
[DisplayOrientation.Portrait] = ScreenOrientation.Portrait,
};
// method SetDeviceOrientation
public partial void SetDeviceOrientation(DisplayOrientation displayOrientation)
{
var currentActivity = ActivityStateManager.Default.GetCurrentActivity();
if (currentActivity is not null)
{
if (_androidDisplayOrientationMap.TryGetValue(displayOrientation, out ScreenOrientation screenOrientation))
{
currentActivity.RequestedOrientation = screenOrientation;
}
}
}
}
In iOS
public partial class DeviceOrientationService
{
public partial DeviceOrientation GetOrientation()
{
UIInterfaceOrientation orientation = UIApplication.SharedApplication.StatusBarOrientation;
bool isPortrait = orientation == UIInterfaceOrientation.Portrait || orientation == UIInterfaceOrientation.PortraitUpsideDown;
return isPortrait ? DeviceOrientation.Portrait : DeviceOrientation.Landscape;
}
private static readonly IReadOnlyDictionary<DisplayOrientation, UIInterfaceOrientation> _iosDisplayOrientationMap =
new Dictionary<DisplayOrientation, UIInterfaceOrientation>
{
[DisplayOrientation.Landscape] = UIInterfaceOrientation.LandscapeLeft,
[DisplayOrientation.Portrait] = UIInterfaceOrientation.Portrait,
};
// implement method SetDeviceOrientation
public partial void SetDeviceOrientation(DisplayOrientation displayOrientation)
{
if (UIDevice.CurrentDevice.CheckSystemVersion(16, 0))
{
var scene = (UIApplication.SharedApplication.ConnectedScenes.ToArray()[0] as UIWindowScene);
if (scene != null)
{
var uiAppplication = UIApplication.SharedApplication;
var test = UIApplication.SharedApplication.KeyWindow?.RootViewController;
if (test != null)
{
test.SetNeedsUpdateOfSupportedInterfaceOrientations();
scene.RequestGeometryUpdate(
new UIWindowSceneGeometryPreferencesIOS(UIInterfaceOrientationMask.Portrait), error => { });
}
}
}
else
{
UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation"));
}
}
}
And use it like this:
namespace InvokePlatformCodeDemos;
using PartialMethodsDeviceOrientationService = InvokePlatformCodeDemos.Services.PartialMethods.DeviceOrientationService;
public partial class NewPage : ContentPage
{
public NewPage()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
// set the Device Orientation here
var deviceOrientationService2 = new PartialMethodsDeviceOrientationService();
deviceOrientationService2.SetDeviceOrientation(DisplayOrientation.Landscape);
}
}
For more information, you can refer to : NET MAUI – Locking Orientation into Portrait or Landscape and Invoke platform code.