简体   繁体   English

使视图适合Xamarin形式的屏幕

[英]Fit Views to Screen in Xamarin Forms

In my cross platform mobile app, I've been sizing all of my views using constant pixel values based on my devices screen size. 在我的跨平台移动应用程序中,我一直在根据设备屏幕尺寸使用恒定像素值来调整所有视图的大小。 The app is meant to be ran on a number of different sizes of screen, though, and I can't find anything on fitting views to the screen size. 该应用程序本应在多种不同尺寸的屏幕上运行,但我无法在适合屏幕尺寸的视图上找到任何东西。

So far my best attempt at this was to store the values of the height and width of the StackLayout on the login page and size the views relative to those values, but I was getting incorrect values of -1 for both. 到目前为止,我对此的最佳尝试是在登录页面上存储StackLayout的高度和宽度的值,并相对于这些值调整视图的大小,但是我都得到了错误的-1值。 Here is the code I used: 这是我使用的代码:

var Test = new StackLayout
          {
                HorizontalOptions = LayoutOptions.CenterAndExpand,
                VerticalOptions = LayoutOptions.CenterAndExpand,
          }
double ScreenHeight = Test.Height;
double ScreenWidth = Test.Width;

You will get -1 because the Width and Height of a view will be set only after the screen is appeared, otherwise Xamarin.Forms wouldn't know these values beforehand. 您将得到-1,因为视图的Width和Height仅在屏幕出现后才设置,否则Xamarin.Forms不会事先知道这些值。

The solution can be implementing the OnAppearing event of any given view (in this case Test) and in that method you can get Width and Height. 该解决方案可以实现任何给定视图的OnAppearing事件(在本例中为Test),并且在该方法中可以获得Width和Height。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM