最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - What prevents a .Net MAUI app from receiving device orientation events? - Stack Overflow

programmeradmin0浏览0评论

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
  • Since your app initially did work, what change did you make to disable it? – Liqun Shen-MSFT Commented 23 hours ago
  • @LiqunShen-MSFT It's been a few weeks since the last time I noticed orientation changes working. I was focused on other features and didn't check orientation. I have made a lot of changes, but I don't remember removing ScreenOrientation from the Activity attribute of MainActivity. Adding it has fixed the problem, so maybe removing it was the cause. – Shadovv Commented 23 hours ago
  • I found a solution! My answer is not authoritative, as I haven't found any documentation to support it, but it does work for me. I won't accept my own answer for at least another day, in case someone comes along with a better (more complete, sourced) answer. – Shadovv Commented 23 hours ago
  • Do you mean OnSizeAllocated not work? Do you use FlyoutPage or NavigationPage? – Liqun Shen-MSFT Commented 57 mins ago
Add a comment  | 

1 Answer 1

Reset to default 0

Apparently, 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!

发布评论

评论列表(0)

  1. 暂无评论