Get current screen width in xamarin form

I am creating one Xamarin Forms project and I am stuck in finding the current screen width according to another device.

I know how this can be done on Android and ios, but I need to find the width in the portable.

+18
source share
7 answers

You can try this one Application.Current.MainPage.Widthin my case, it worked, and I can find the width of the device in the most portable project.

+40
source

Generic code (in App.xaml.cs)

public static int ScreenHeight {get; set;}
public static int ScreenWidth {get; set;}

part of Android (in MainActivity.cs, in the OnCreate method)

App.ScreenHeight = (int) (Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Density);
App.ScreenWidth = (int) (Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density);

part of iOS (in the AppDelegate.cs application, in the FinishedLaunching method)

App.ScreenHeight = (int)UIScreen.MainScreen.Bounds.Height;
App.ScreenWidth = (int)UIScreen.MainScreen.Bounds.Width;

, App.ScreenHeight App.ScreenWidth , , .

+25

, , , Xamarin.Essentials NuGet, DeviceDisplay, .

.

:

// Get Metrics
var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;

// Orientation (Landscape, Portrait, Square, Unknown)
var orientation = mainDisplayInfo.Orientation;

// Rotation (0, 90, 180, 270)
var rotation = mainDisplayInfo.Rotation;

// Width (in pixels)
var width = mainDisplayInfo.Width;

// Height (in pixels)
var height = mainDisplayInfo.Height;

// Screen density
var density = mainDisplayInfo.Density;
+18

Width, ContentPage. ,

protected override void OnAppearing()
{
    base.OnAppearing();
    double w = this.Width;
}

, . , , -1.

https://developer.xamarin.com/api/property/Xamarin.Forms.VisualElement.Width/

+6

, / ( xAML)

(, App.xaml.cs):

using System;
using Xamarin.Forms;

namespace YourAppName
{

    ...

    public struct Constant
    {
        public static double ScreenWidth = Application.Current.MainPage.Width;
        public static double ScreenHeight = Application.Current.MainPage.Height;
    }
}

#:

var labelWidth = Constant.ScreenHeight * 0.2;
// then do something with labelWidth

XAML:

<Label Text="Text with insane font size" FontSize="{Binding Source={x:Static local:Constant.ScreenWidth}}"/>
+2

"OnSizeAllocated" , . , , , .

:

public static class ScreenDimensions
{
    public static double Height { get; set; }
    public static double Width { get; set; }
}

, :

protected override void OnSizeAllocated(double width, double height)
{
    base.OnSizeAllocated(width, height);
    ScreenDimensions.Height = height;
    ScreenDimensions.Width = width;
}
+2

, - xamarin.forms.Device.

               Size size = Device.Info.PixelScreenSize;

            if (size.Width <= 720 && size.Height <= 1280)
            {
                stkLayoutTop.Margin = new Thickness(5, 30, 10, 0);
            }
            else
            {
                stkLayoutTop.Margin = new Thickness(5, 50, 10, 0);
            }
0

Source: https://habr.com/ru/post/1650889/


All Articles