Hello,
Welcome to Microsoft Q&A!
By tapping the button I should change the orientation even though I stay in Landscape I should show a portrait view.
We can use messaging center to force Landscape or Portrait on each platform.
Forms
private void Button_Clicked(object sender, EventArgs e)
{
MessagingCenter.Send<object,string>(this, "ChangeOrientation", "Landscape");
}
Android
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
MessagingCenter.Subscribe<object,string>(this, "ChangeOrientation", (sender,args) =>
{
if(args == "Landscape")
{
RequestedOrientation = ScreenOrientation.Unspecified;
}
else if (args == "Portrait")
{
RequestedOrientation = ScreenOrientation.Portrait;
}
});
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}
iOS
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
MessagingCenter.Subscribe<object, string>(this, "ChangeOrientation", (sender, args) =>
{
if (args == "Landscape")
{
UIDevice.CurrentDevice.SetValueForKey(NSNumber.FromNInt((int)(UIInterfaceOrientation.LandscapeLeft)), new NSString("orientation"));
}
else if (args == "Portrait")
{
UIDevice.CurrentDevice.SetValueForKey(NSNumber.FromNInt((int)(UIInterfaceOrientation.Portrait)), new NSString("orientation"));
}
});
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
Best Regards,
Cole Xia
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.