You can do this by creating a dependency using DependencyService
for specific platforms.
Try doing this:
Interface
public interface IOrientationService
{
void Landscape();
void Portrait();
}
For 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:
[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!