I want my .Net 9 MAUI app (initially targeting Android) to respond to changes in device orientation. Initially, this DID work, but at some point I must have made a change and didn't notice that orientation changes were no longer triggering events. I have verified that both the emulator (Pixel 7 pro, API 35) and a physical device (Samsung S23 Ultra, API 34) have Auto-Rotate enabled and orientation changes ARE working for other apps.
At first, I was using ContentPage.SizeChanged, which was fine for individual pages.
public partial class SplashScreen : ContentPage
{
public SplashScreen()
{
InitializeComponent();
BindingContext = this;
this.SizeChanged += OnPageSizeChanged;
}
public void OnPageSizeChanged(object sender, EventArgs e)
{
Debug.WriteLine("OnPageSizeChanged triggered");
}
}
When I noticed that wasn't working anymore (OnPageSizeChanged was not triggered at all), I tried overriding OnSizeAllocated, as suggested here. Currently, this method is executed only once (when the app starts), but never in response to an orientation change.
Next, I tried using MainDisplayInfoChanged:
public partial class SplashScreen : ContentPage
{
public SplashScreen()
{
InitializeComponent();
BindingContext = this;
DeviceDisplay.Current.MainDisplayInfoChanged += (object sender, DisplayInfoChangedEventArgs e) =>
{
Debug.WriteLine("MainDisplayInfoChanged triggered");
}
}
}
This didn't work at all; MainDisplayInfoChanged is not triggered during startup or orientation change, either as a lambda or as a regular event handler.
Searching for solutions around MainDisplayInfoChanged, I found this question, which suggested overriding OnConfigurationChanged in my Activity.
[Activity(Theme = "@style/MyTheme.Splash", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.KeyboardHidden | ConfigChanges.ScreenLayout | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
{
public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig)
{
Debug.WriteLine("MainActivity OnConfigurationChanged");
base.OnConfigurationChanged(newConfig);
}
}
I doubt I need most of those ConfigChanges, but I have tried Orientation and ScreenSize together, separately, and even removing the entire ConfigurationChanges parameter. I have also tried the same sets of ConfigChanges values in the AndroidManifest.xml file in the Application section, thinking that would apply to all Activities.
Despite everything I've tried, I have not found anything that causes my app to respond to orientation changes or even execute the relevant events. So, my question: what is preventing my app from responding to, or even being aware of, orientation changes?
I want my .Net 9 MAUI app (initially targeting Android) to respond to changes in device orientation. Initially, this DID work, but at some point I must have made a change and didn't notice that orientation changes were no longer triggering events. I have verified that both the emulator (Pixel 7 pro, API 35) and a physical device (Samsung S23 Ultra, API 34) have Auto-Rotate enabled and orientation changes ARE working for other apps.
At first, I was using ContentPage.SizeChanged, which was fine for individual pages.
public partial class SplashScreen : ContentPage
{
public SplashScreen()
{
InitializeComponent();
BindingContext = this;
this.SizeChanged += OnPageSizeChanged;
}
public void OnPageSizeChanged(object sender, EventArgs e)
{
Debug.WriteLine("OnPageSizeChanged triggered");
}
}
When I noticed that wasn't working anymore (OnPageSizeChanged was not triggered at all), I tried overriding OnSizeAllocated, as suggested here. Currently, this method is executed only once (when the app starts), but never in response to an orientation change.
Next, I tried using MainDisplayInfoChanged:
public partial class SplashScreen : ContentPage
{
public SplashScreen()
{
InitializeComponent();
BindingContext = this;
DeviceDisplay.Current.MainDisplayInfoChanged += (object sender, DisplayInfoChangedEventArgs e) =>
{
Debug.WriteLine("MainDisplayInfoChanged triggered");
}
}
}
This didn't work at all; MainDisplayInfoChanged is not triggered during startup or orientation change, either as a lambda or as a regular event handler.
Searching for solutions around MainDisplayInfoChanged, I found this question, which suggested overriding OnConfigurationChanged in my Activity.
[Activity(Theme = "@style/MyTheme.Splash", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.KeyboardHidden | ConfigChanges.ScreenLayout | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
{
public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig)
{
Debug.WriteLine("MainActivity OnConfigurationChanged");
base.OnConfigurationChanged(newConfig);
}
}
I doubt I need most of those ConfigChanges, but I have tried Orientation and ScreenSize together, separately, and even removing the entire ConfigurationChanges parameter. I have also tried the same sets of ConfigChanges values in the AndroidManifest.xml file in the Application section, thinking that would apply to all Activities.
Despite everything I've tried, I have not found anything that causes my app to respond to orientation changes or even execute the relevant events. So, my question: what is preventing my app from responding to, or even being aware of, orientation changes?
Share Improve this question edited yesterday Shadovv asked 2 days ago ShadovvShadovv 618 bronze badges 4 |1 Answer
Reset to default 0Apparently, specifying a ScreenOrientation in MainActivity is required for a .Net MAUI Android app to receive orientation-related events. I say "apparently" because I haven't seen this requirement in any documentation, including the Android Platform documentation for ScreenOrientation.
Adding ScreenOrientation = ScreenOrientation.FullUser to the Activity Attribute for MainActivity caused my app to begin receiving ALL of the events listed in the question!
OnSizeAllocated
not work? Do you useFlyoutPage
orNavigationPage
? – Liqun Shen-MSFT Commented 57 mins ago