相关疑难解决方法(0)

Xamarin.Forms:如何使用相对布局居中视图?`Width`和`Height`返回-1

当试图在Xamarin.Forms中使用控件的HeightWidth属性时,都返回-1,这会导致相对布局在屏幕上偏离中心.

var mainLayout = new RelativeLayout();

//Add the Switch to the center of the screen
mainLayout.Children.Add(mySwitch,
    Constraint.RelativeToParent(parent => parent.Width / 2 - mySwitch.Width / 2),
    Constraint.RelativeToParent(parent => parent.Height / 2 - mySwitch.Height / 2));

//Add a Label below the switch
mainLayout.Children.Add(switchLabel,
    Constraint.RelativeToParent(parent => parent.Width / 2 - switchLabel.Width / 2),
    Constraint.RelativeToView(mySwitch, (parent, view) => view.Y + mySwitch.Height + 10));

Content = mainLayout;
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

c# xamarin xamarin.forms

7
推荐指数
1
解决办法
6519
查看次数

标签 统计

c# ×1

xamarin ×1

xamarin.forms ×1