Visual studio 为什么赢了';在Xamarin Forms项目的相对工作中是否存在这些约束?
布局如下:Visual studio 为什么赢了';在Xamarin Forms项目的相对工作中是否存在这些约束?,visual-studio,xaml,xamarin,xamarin.forms,Visual Studio,Xaml,Xamarin,Xamarin.forms,布局如下: <?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:views="clr-namespace:Test.Views" x:Class="Test.Views.MainPage"> <S
<?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:views="clr-namespace:Test.Views" x:Class="Test.Views.MainPage">
<StackLayout VerticalOptions="FillAndExpand">
<BoxView Color="Yellow" HeightRequest="25" VerticalOptions="Start"></BoxView>
<RelativeLayout x:Name="mainView" BackgroundColor="Red" VerticalOptions="StartAndExpand">
<BoxView x:Name="boxA" Color="Blue" WidthRequest="50" HeightRequest="50"
RelativeLayout.XConstraint="{Binding BoxAXConstraint}"
RelativeLayout.YConstraint="{Binding BoxAYConstraint}">
</BoxView>
<BoxView x:Name="boxB" Color="Green" WidthRequest="50" HeightRequest="50"
RelativeLayout.XConstraint="{Binding BoxBXConstraint}"
RelativeLayout.YConstraint="{Binding BoxBYConstraint}">
</BoxView>
</RelativeLayout>
</StackLayout>
</ContentPage>
这是一个在Android和iOS上都重现了这个问题的例子
RelativeLayout对可以用XAML表示的内容有限制-例如,不能在同一约束中引用两个不同元素的布局属性。你完全可以用C#来表达更多。在这种情况下,您应该能够在XAML中几乎同等地执行操作:
<BoxView x:Name="boxB" Color="Green" WidthRequest="50" HeightRequest="50"
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Width,Factor=1,Constant=-50}"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Height,Factor=1,Constant=-50}">
</BoxView>
AbsoluteLayout.LayoutBounds="1,1,-1,-1" AbsoluteLayout.LayoutFlags="PositionProportional"
如果它是一个测试项目的bug,那么将其作为问题发布在Xamarin.Forms repo上:我发布的代码是我实际布局的精简版本。在我的布局中,我还为两个小框添加了一些填充,这取决于它们的尺寸,并且在右上角的小框左侧旁边有第三个框,因此其位置取决于右上角框的尺寸。这就是我选择使用RelativeLayout的原因。鉴于此,您可能会被C#中定义的RelativeLayout和约束所困扰。唯一值得考虑的替代方案是通过定义一个布局(StackLayout、RelativeLayout、Grid等)添加一些嵌套,该布局包含“右上角的所有内容”,另一个包含“右下角的所有内容”然后使用AbsoluteLayout将这两个容器布局放在整个页面的正确位置。我将尝试一下。谢谢你的建议!
AbsoluteLayout.LayoutBounds="1,1,-1,-1" AbsoluteLayout.LayoutFlags="PositionProportional"