How wide is the master page of a Master-Detail Page in Xamarin.Forms?

≯℡__Kan透↙ 提交于 2019-12-10 17:42:31

问题


Depending on the screen size (and device idiom?) the width of the master page varies: On phones it is about 80 % of the screen width, while on tablets it seems to be a constant dimension like 320 dp.

Does anybody know a general formula for this value? I'd like to use it for laying out some elements during construction time, when the Width property isn't set, yet.

Edit: I know how to get the current screen size. But how does the width of the presented master page of Xamarin.Form's master-detail page relate to it? It doesn't cover the whole screen, but fills a different fraction of it depending on the device.


回答1:


You could request the device's 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);
        }

    }


来源:https://stackoverflow.com/questions/40217003/how-wide-is-the-master-page-of-a-master-detail-page-in-xamarin-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!