【问题标题】:Get current screen width in xamarin forms以 xamarin 形式获取当前屏幕宽度
【发布时间】:2016-12-17 21:57:23
【问题描述】:

我正在创建一个 Xamarin Forms 项目,但我陷入了根据不同设备查找当前屏幕宽度的困境。

我知道如何在 android 和 ios 中完成它,但需要一种方法来找到可移植的宽度。

【问题讨论】:

    标签: xamarin width xamarin.forms


    【解决方案1】:

    你可以试试这个Application.Current.MainPage.Width,在我的情况下这很有效,我可以在便携式项目本身中找到设备宽度。

    【讨论】:

    • 这很可能相当于直接输入 this.Width 。生命周期时间在这里是相关的。
    • 这很好用,除非在 MainPage 尚未呈现时的初始加载。
    • 我在 Samsung S20 - Android 11 上失败了。因为它被初始化为 -1 。您现在应该更改为 @Evs 解决方案。
    【解决方案2】:

    我知道这是一个旧线程,但值得一提的是,最近发布了 Xamarin.Essentials NuGet 包,其中有一个有用的类 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;
    
    // Width (in xamarin.forms units)
    var xamarinWidth = width / mainDisplayInfo.Density;
    
    // Height (in pixels)
    var height = mainDisplayInfo.Height;
    
    // Screen density
    var density = mainDisplayInfo.Density;
    

    【讨论】:

    • DeviceDisplay.ScreenMetrics 好像改名为DeviceDisplay.MainDisplayInfo
    • 我必须将mainDisplayInfo.Width/Height 除以mainDisplayInfo.Density 才能得到我正在寻找的值。
    • 除以密度是正确的宽度。使用 Application.Current.MainPage.Width; 可以达到相同的结果;
    • 不错的答案,但请注意,如果您使用的设备上的应用程序窗口大小不是完整的设备宽度/高度(例如 Chromebook),那么这会让您感到厌烦
    【解决方案3】:

    通用代码(在 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 启动时被初始化,然后您就可以在通用代码中的任何地方使用它们。

    【讨论】:

    • 这为我返回 0
    • 比 Xamarin.Essentials 更好用(它给了我相当随机的数字)!
    【解决方案4】:

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

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

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

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

    【讨论】:

    • Task.Delay 不是等待获取 size 属性的好方法。相反,我宁愿订阅这样的视图的 EventHandler 'SizeChanged':SizeChanged += (sender, e) => {...};
    【解决方案5】:

    我想在共享代码中创建一个静态结构,以便我可以轻松访问屏幕宽度/高度值(以及在 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;
        }
    }
    

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

    【讨论】:

      【解决方案6】:

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

      【讨论】:

        【解决方案7】:

        我猜最好的方法是使用 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);
                    }
        

        【讨论】:

        • 这不是真正的像素大小
        猜你喜欢
        • 1970-01-01
        • 2017-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多