以xamarin形式获取当前屏幕宽度

问题描述 投票:15回答:7

我正在创建一个Xamarin Forms项目,我根据不同的设备找不到当前的屏幕宽度。

我知道如何在android和ios中完成它,但需要一种方法来在便携式中找到宽度。

xamarin width xamarin.forms
7个回答
38
投票

您可以尝试这个Application.Current.MainPage.Width在我的情况下这工作,我能够在便携式项目本身找到设备宽度。


23
投票

常用代码(在App.xaml.cs中)

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

Android部分(在MainActivity.cs中,在OnCreate方法中)

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

iOS部分(在AppDelegate.cs中,在FinishedLaunching方法中)

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

因此App.ScreenHeight和App.ScreenWidth将在App启动时初始化,然后您就可以在公共代码中的任何位置使用它们。


15
投票

我知道这是一个旧线程,但值得一提的是,最近Xamarin.Essentials NuGet pakage已经发布,并且有一个有用的类DeviceDisplay在那里应该为你处理这个任务。

文档可以找到here

用法示例:

// 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;

6
投票

当您在ContentPage中时,可以使用Width属性。例如,

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

请注意,这是在布局阶段设置的。因此,您只能在页面初始化后使用它,否则它是-1。

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


1
投票

我会在共享代码中创建一个静态结构,因此我可以轻松访问屏幕宽度/高度值(以及在xAML中进行绑定)

app命名空间中的结构(例如,在App.xaml.cs中):

using System;
using Xamarin.Forms;

namespace YourAppName
{
    public static class HelperFunction
    {
        ... 

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

        ...
    }
}

在C#中检索屏幕宽度:

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}}"/>

1
投票

另一种方法是为页面使用覆盖“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;
}

0
投票

我猜最好的方法是使用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);
            }
© www.soinside.com 2019 - 2024. All rights reserved.