How wide is the main page of the Master-Detail page in Xamarin.Forms?

Depending on the screen size (and the idiom of the device?), The width of the main page changes: on phones it is about 80% of the screen width, and on tablets it is a constant size, for example 320 dp.

Does anyone know a general formula for this value? I would like to use it to highlight some elements during construction, when the Width property is not already set.

Edit: I know how to get the current screen size . But how does the width of the presented main page of the Xamarin.Form master part page relate to it? It does not cover the entire screen, but fills it depending on the device.

+2
source share
1 answer

You can request the actual screen width, height and scale factor.

(From 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 Forms 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); } } 
0
source

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


All Articles