【问题标题】:Why doesn't ParentView.Width get me the ParentView's Width in Xamarin.Forms?为什么 ParentView.Width 不能让我在 Xamarin.Forms 中获得 ParentView 的宽度?
【发布时间】:2015-09-24 13:13:54
【问题描述】:

真的就这么简单。我想要 Xamarin.Forms 中整个屏幕的宽度。你会认为当我调用ParentView.Width 时,它会为我返回父视图的宽度。相反,它返回了一个异常。 “对象引用不是对象的实例。”

如何获取父视图的宽度,以便我可以根据该宽度操作子元素。例如:

int xPos = this.Width*0.5;
//or 
int xPos = ParentView.Width*0.5;

【问题讨论】:

    标签: c# android ios xamarin xamarin.forms


    【解决方案1】:

    如果需要屏幕的宽度,可以使用

    在安卓上:

    Resources.DisplayMetrics.WidthPixels

    或(更有可能):

    Resources.DisplayMetrics.WidthPixels / 
                Resources.DisplayMetrics.Density
    

    如果你希望它是倾斜的而不是像素。 (所有 Xamarin.Forms 控件的 WidthRequest 和 HeightRequest 都使用 dip)。

    在 iOS 上:

    UIScreen.MainScreen.Bounds.Width
    

    你也可以做一个接口,使用DependencyService将每个平台的宽度返回给你的共享项目

    【讨论】:

    猜你喜欢
    • 2019-06-05
    • 1970-01-01
    • 2019-02-18
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-08
    相关资源
    最近更新 更多