How to know if your Xamarin.Forms application is running on a mobile phone or on an emulator

Yannick Nfankou 0 Reputation points
2023-10-07T06:19:23.57+00:00

Good morning , How to know if your Xamarin.Forms application is running on a mobile phone or on an emulator like Bluestacks. , ,NoxPlayer, MEmu Play, LDPLayer, GameLoop, Anbox, Android Studio. I need to know if my application is running on a PC or Phone. Sincerely ,

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,326 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 39,391 Reputation points Microsoft Vendor
    2023-10-09T02:20:26.1766667+00:00

    Hello,

    In Xamairn, you could implement this requirement using the official DeviceType.

    Please refer to the following documentation and sample code.

    private void Button_Clicked(object sender, EventArgs e)
    {
        switch (DeviceInfo.DeviceType)
        {
            case DeviceType.Unknown:
                break;
            case DeviceType.Physical:
                Console.WriteLine("device");
                break;
            case DeviceType.Virtual:
                Console.WriteLine("emulator");
                break;
            default:
                break;
        }
    }
    

    Best Regards,

    Alec Liu.


    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.

    0 comments No comments