带有Xamarin表单的App.ScreenWidth

Joh*_*ore 3 xamarin xamarin.forms

引用在...处找到的代码突出显示地图上的路线

他们展示...

var customMap = new CustomMap
{
    WidthRequest = App.ScreenWidth
};
Run Code Online (Sandbox Code Playgroud)

App.ScreenWidth不再可用.它被取代了Application.Current.MainPage.Width吗?

Sus*_*ver 8

在该演示中,App.ScreenWidth并且App.ScreenHeight是在定义静态变量App类,并分配到native项目:

iOS应用项目:

App.ScreenWidth = UIScreen.MainScreen.Bounds.Width;
App.ScreenHeight = UIScreen.MainScreen.Bounds.Height
Run Code Online (Sandbox Code Playgroud)

Android应用程序项目:

App.ScreenWidth = (width - 0.5f) / density;
App.ScreenHeight = (height - 0.5f) / density;
Run Code Online (Sandbox Code Playgroud)

参考:https://github.com/xamarin/recipes/search? p = 2& q = ScreenWidth & utf8 =


Jay*_*tel 6

在PCL中获取设备高度和宽度的最简单,最准确的方法:

using Xamarin.Forms;

namespace ABC
{
    public class MyPage : ContentPage
    {
        private double _width;
        private double _height;

        public MyPage()
        {
            Content = new Label 
            {
                WidthRequest = _width,
                Text = "Welcome to Xamarin.Forms!"
            };
        }

        protected override void OnSizeAllocated(double width, double height)
        {
            base.OnSizeAllocated(width, height);
            _width = width;
            _height = height;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)