question

dg2k avatar image
0 Votes"
dg2k asked dg2k commented

Getting Display Info with .NET MAUI on Windows Platform

I want to get Display Info in my .NET MAUI app such as screen width, height, pixel density, etc.

A similar question was answered by @GeraldVersluis about 4 months ago here but I get no values for the display parameters (only default 0's for int and Unknowns for Enums are returned).

Has anything changed in the interim in the Microsoft.Maui.Essentials.DeviceDisplay.MainDisplayInfo namespace?
Please help.


dotnet-maui
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi anonymous user-4673 , DeviceDisplay.ios in the Microsoft.Maui.Essentials.DeviceDisplay.MainDisplayInfo updated 19 days ago, DeviceDisplay.android.cs updated 17 days ago, refer: https://github.com/dotnet/maui/blob/main/src/Essentials/src/DeviceDisplay/DeviceDisplay.ios.cs
https://github.com/dotnet/maui/blob/main/src/Essentials/src/DeviceDisplay/DeviceDisplay.android.cs
I'm not sure which platform you are testing on, and how you are calling this method, I test on Android/ Windows/iOS and call the method on a click event in MainPage, the following code works on my side. I have to say, I can't reproduce your issue. Would you mind sharing more information with me?

 var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
 CounterLabel.Text = $"width: {mainDisplayInfo.Width} height :{mainDisplayInfo.Height} density:{mainDisplayInfo.Density}";
0 Votes 0 ·
dg2k avatar image dg2k WenyanZhang-MSFT ·

Thank you @WenyanZhang-MSFT.
Your example (based on user initiated click event) works for me as well, but very strange that the method call doesn't return values where such values are required in Application and Page life cycles.

The values are not returned at the following points:

  • App() constructor (in App.xaml.cs)

  • App OnStart()

  • MainPage() constructor (in MainPage.xaml.cs)

  • MainPage OnAppearing()

  • MainPage - event hook to Appearing(object sender, EventArgs e)

In Xamarin Forms, the values are available as early as the App() constructor (of course, pulled in from respective native method from platform start page). Unless I am missing something obvious, such as something essential that needs injecting into MauiProgram.cs, I cannot get the DisplayInfo where I need it in my programming flow.

My test is on Windows platform.

Update:

  • I ran the above tests on Android Emulator and all work as expected.

  • The issue for definite is now with the Windows platform though haven't checked on iOS -- which means that I may need to qualify my question wrt Windows.


0 Votes 0 ·

1 Answer

WenyanZhang-MSFT avatar image
1 Vote"
WenyanZhang-MSFT answered dg2k commented

Hello,

You can try to create a task that can be done after a delay and then call MainDisplayInfo, but it doesn't work if you want to get MainDisplayInfo early in the application life cycle.

 protected override void OnAppearing()// works on App() constructor , App OnStart(), MainPage() constructor
 {
     base.OnAppearing();
     MainThread.BeginInvokeOnMainThread(async () => await DelayedShowWidth());
 }
 private async Task DelayedShowWidth()
 {
     await Task.Delay(500);
     var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
    // do something       
 }

There is an issue discussing MainDisplayInfo in AppShell constructor on GitHub: https://github.com/dotnet/maui/issues/6976, you could refer to this workaround :https://github.com/dotnet/maui/issues/6976#issuecomment-1122175778 and follow the progress.


Best Regards,
Wenyan Zhang


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you @WenyanZhang-MSFT for the answer with more leads.

Certainly the link you provided is the same issue I am having, so I am not alone.
By way of feedback, I do hope such basic API calls (in this case Display Information) will be readily available rather than being relegated to unnecessary workarounds.

In contrast, the display info is readily available for mobile devices (see above) and it's as if the Windows platform is receiving less priority??


0 Votes 0 ·