Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

DeviceDisplay.MainDisplayInfoChanged only working on second try

Mo719Mo719 Member ✭✭

Hi community,

I hope you can help me with my issue:

I implemented an event handler for the Event DeviceDisplay.MainDisplayInfoChanged (Xamarin.Essentials).
Unfortunatly, it's only working after I turned the phone once. So I turn the phone from Portrait into Landscape --> EventHandler isn't activated.
If I turn the phone again, the Event is triggered and the code of my EventHandler runs.

Is this a known issue? Is there a way to fix this?

My emulator is a Pixel 3 XL w/ Android Pie.

Thanks for your help :)

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    When we check official document DeviceDisplay.MainDisplayInfoChanged,we will see:

    So the event will been triggered when the main display info changes.

    When we first run the app on our device, the main display info doesn't change, so the event will not been triggered .

  • Mo719Mo719 Member ✭✭

    @jezh Thanks for your answer. The point is the following: I start the app first. Then I turn the device. The event isn't triggered. I turn the device again --> Event is triggered

  • jezhjezh Member, Xamarin Team Xamurai

    Sorry,I couldn't reproduce this question.I tested on Google Pixel (android 8.1).
    How did you add the code to your app?
    This is my test code, you can refer to it.

        public partial class App : Application
        {
            public App()
            {
                InitializeComponent();
    
                MainPage = new MainPage();
    
                DeviceDisplay.MainDisplayInfoChanged += OnMainDisplayInfoChanged;
            }
    
            private void OnMainDisplayInfoChanged(object sender, DisplayInfoChangedEventArgs e)
            {
                DisplayInfo displayInfo = e.DisplayInfo;
    
                System.Diagnostics.Debug.WriteLine("********** OnMainDisplayInfoChanged is running.............");           
            }
    
            // other code
        }
    
  • Mo719Mo719 Member ✭✭
    edited March 31

    So I try to use this code to push another page on the navigation stack

    public partial class ThisDayPage : ContentPage
        {
            public ThisDayPage()
            {
                InitializeComponent();
                DeviceDisplay.MainDisplayInfoChanged += OnMainDisplayInfoChanged;
            }
    
            private async void OnMainDisplayInfoChanged(object sender, DisplayInfoChangedEventArgs e)
            {
                await Navigation.PushModalAsync(new ThisWeekPage());
            }
        // other code
         }
    

    still not working for me unfortunatly...
    This solution works for me btw:

    protected async override void OnSizeAllocated(double width, double height)
            {
                base.OnSizeAllocated(width, height);
                if (width < height)
                {
                    //empty
                }
                else if (height < width)
                {
                    await Navigation.PushModalAsync(new ThisWeekPage());
                }
                else
                {
                    throw new ArgumentOutOfRangeException();
                }
            }
    

    Thanks for your help! If you know why the code with Xamarin.Essentials isn't working, please let me know :)

Sign In or Register to comment.