【发布时间】:2018-06-05 21:12:05
【问题描述】:
我是Xamarin.Forms 的新手(Swift 是我的强项)。为了让我的应用程序适用于所有屏幕尺寸,我使用RelativeLayout。唯一的问题是,我只能找出如何设置宽度和高度。
我要做的是设置顶部和底部约束。
这是我在Swift可以做的:
let redBox = UIView()
redBox.translatesAutoresizingMaskIntoConstraints = false
redBox.backgroundColor = .red
let blueBox = UIView()
blueBox.translatesAutoresizingMaskIntoConstraints = false
blueBox.backgroundColor = .blue
view.addSubview(redBox)
view.addSubview(blueBox)
redBox.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
redBox.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
redBox.heightAnchor.constraint(equalToConstant: 150).isActive = true
blueBox.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
blueBox.topAnchor.constraint(equalTo: redBox.bottomAnchor).isActive = true
blueBox.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
这将导致以下屏幕:
我将如何在Xamarin.Forms 中实现这一目标?
我只能找出如何做宽度和高度限制。我也想以编程方式而不是 XAML 或拖放。
这是我发现的C#Xamarin.Forms代码:
var layout = new RelativeLayout();
Content = layout;
var aquaBox = new BoxView()
{
Color = Color.Aqua
};
var silverBox = new BoxView
{
Color = Color.Silver
};
aquaBox.HeightRequest = 150;
layout.Children.Add(aquaBox,
widthConstraint: Constraint.RelativeToParent(parent => parent.Width)
);
layout.Children.Add(silverBox,
yConstraint: Constraint.RelativeToView(aquaBox, (RelativeLayout, element) => element.Height)
);
XAML:
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Test"
x:Class="Test.TestPage">
</ContentPage>
这段代码的结果是:
需要什么C#Xamarin.Forms 代码才能获得与Swift 相同的视图?
注意:我想要Xamarin.Forms 代码,而不是Android、Windows 和iOS 的单独代码。
【问题讨论】:
标签: c# macos visual-studio xamarin.forms