【问题标题】:App.ScreenWidth with Xamarin FormsApp.ScreenWidth 与 Xamarin 窗体
【发布时间】:2017-05-08 14:28:04
【问题描述】:

参考代码位于...Highlight a Route on a Map

他们显示...

var customMap = new CustomMap
{
    WidthRequest = App.ScreenWidth
};

App.ScreenWidth 不再可用。被Application.Current.MainPage.Width替换了吗?

【问题讨论】:

标签: xamarin xamarin.forms


【解决方案1】:

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

iOS 应用项目:

App.ScreenWidth = UIScreen.MainScreen.Bounds.Width;
App.ScreenHeight = UIScreen.MainScreen.Bounds.Height

Android 应用项目:

App.ScreenWidth = (width - 0.5f) / density;
App.ScreenHeight = (height - 0.5f) / density;

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

【讨论】:

    【解决方案2】:

    在 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;
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      我相信替代方案,即问题中建议的 OP,是正确答案。 look here

      更新:此解决方案的一个问题是,当 MainPage 尚未出现时,它将返回 -1。在这种情况下,@SushiHangover 解决方案更好。

      【讨论】:

        猜你喜欢
        • 2019-08-25
        • 2019-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-28
        • 2017-12-01
        • 2017-11-11
        相关资源
        最近更新 更多