Getting Display Info with .NET MAUI on Windows Platform

dg2k 1,386 Reputation points
2022-05-16T07:59:11.867+00:00

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 @Gerald Versluis 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.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,859 questions
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 26,221 Reputation points Microsoft Vendor
    2022-05-18T09:37:53.05+00:00

    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 person found this answer helpful.

0 additional answers

Sort by: Most helpful