【发布时间】:2016-12-17 21:57:23
【问题描述】:
我正在创建一个 Xamarin Forms 项目,但我陷入了根据不同设备查找当前屏幕宽度的困境。
我知道如何在 android 和 ios 中完成它,但需要一种方法来找到可移植的宽度。
【问题讨论】:
标签: xamarin width xamarin.forms
我正在创建一个 Xamarin Forms 项目,但我陷入了根据不同设备查找当前屏幕宽度的困境。
我知道如何在 android 和 ios 中完成它,但需要一种方法来找到可移植的宽度。
【问题讨论】:
标签: xamarin width xamarin.forms
你可以试试这个Application.Current.MainPage.Width,在我的情况下这很有效,我可以在便携式项目本身中找到设备宽度。
【讨论】:
我知道这是一个旧线程,但值得一提的是,最近发布了 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 才能得到我正在寻找的值。
通用代码(在 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 启动时被初始化,然后您就可以在通用代码中的任何地方使用它们。
【讨论】:
您可以在 ContentPage 中使用 Width 属性。例如,
protected override void OnAppearing()
{
base.OnAppearing();
double w = this.Width;
}
请注意,这是在布局阶段设置的。所以,只能在页面初始化后使用,否则为-1。
https://developer.xamarin.com/api/property/Xamarin.Forms.VisualElement.Width/
【讨论】:
我想在共享代码中创建一个静态结构,以便我可以轻松访问屏幕宽度/高度值(以及在 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}}"/>
【讨论】:
另一种方法是为您的页面使用覆盖“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;
}
【讨论】:
我猜最好的方法是使用 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);
}
【讨论】: