根据屏幕尺寸(和设备惯用语?),母版页的宽度会有所不同:在手机上它大约是屏幕宽度的80%,而在平板电脑上,它似乎是一个恒定的尺寸,如320 dp。
有人知道这个值的通用公式吗?我想在构建期间使用它来布置一些元素,当时Width
属性没有设置。
修改 我知道how to get the current screen size。但是,Xamarin.Form的主 - 详细页面的主页的宽度与它有什么关系呢?它不会覆盖整个屏幕,但会根据设备填充不同的部分。
答案 0 :(得分:0)
您可以请求设备的实际屏幕宽度,高度和比例因子。
(来自https://github.com/mattregul/Xamarin_GetDeviceScreensize)
iOS AppDelegate.cs
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
// Store off the device sizes, so we can access them within Xamarin Forms
App.DisplayScreenWidth = (double)UIScreen.MainScreen.Bounds.Width;
App.DisplayScreenHeight = (double)UIScreen.MainScreen.Bounds.Height;
App.DisplayScaleFactor = (double)UIScreen.MainScreen.Scale;
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
}
Android MainActivity.cs
[Activity(Label = "Xamarin_GetDeviceScreensize.Droid", Icon = "@drawable/icon", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
// Store off the device sizes, so we can access them within Xamarin Forms
App.DisplayScreenWidth = (double)Resources.DisplayMetrics.WidthPixels / (double)Resources.DisplayMetrics.Density; // Width = WidthPixels / Density
App.DisplayScreenHeight = (double)Resources.DisplayMetrics.HeightPixels / (double)Resources.DisplayMetrics.Density; // Height = HeightPixels / Density
App.DisplayScaleFactor = (double)Resources.DisplayMetrics.Density;
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
}
Xamarin表示App.cs
public class App : Application
{
public static double DisplayScreenWidth;
public static double DisplayScreenHeight;
public static double DisplayScaleFactor;
public App()
{
string ScreenDetails = Device.OS.ToString() + " Device Screen Size:\n" +
$"Width: {DisplayScreenWidth}\n" +
$"Height: {DisplayScreenHeight}\n" +
$"Scale Factor: {DisplayScaleFactor}";
// The root page of your application
var content = new ContentPage
{
Title = "Xamarin_GetDeviceScreensize",
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
Children = {
new Label {
HorizontalTextAlignment = TextAlignment.Center,
FontSize = Device.GetNamedSize (NamedSize.Large, typeof(Label)),
Text = ScreenDetails
}
}
}
};
MainPage = new NavigationPage(content);
}
}