C# 使用Xamarin XAML表单的堆栈布局噩梦

C# 使用Xamarin XAML表单的堆栈布局噩梦,c#,xaml,xamarin,xamarin.forms,C#,Xaml,Xamarin,Xamarin.forms,我有以下代码: <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" Margin="0,10,0,10" BackgroundColor="Green" Padding="5,5,5,5"> <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand" Margin="0,0,0,0" Ba

我有以下代码:

  <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" Margin="0,10,0,10" BackgroundColor="Green" Padding="5,5,5,5">
        <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand" Margin="0,0,0,0" BackgroundColor="Blue">
              <Label Text="SHOW COMPLETED TASKS" BackgroundColor="Red" Style="{StaticResource lblSubHeading_Black}" />
        </StackLayout>
        <Switch x:Name="CompletedJobsSwitch" Toggled="CompletedJobsSwitch_Toggled" HorizontalOptions="EndAndExpand" IsToggled="{Binding isOn}" BackgroundColor="Yellow"/>
  </StackLayout>

这一切都加载良好,但当应用程序加载时,它会显示,但开关不是正确的。为什么?这真的很烦人,看起来很不一致 我看过了,但这对我不起作用

有什么想法吗?

用这个

<Grid BackgroundColor="Green">
  <Grid.RowDefinitions>
    <RowDefinition Height="auto" />
  </Grid.RowDefinitions>

  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="auto" />
  </Grid.ColumnDefinitions>
  <Label Text="SHOW COMPLETED TASKS" BackgroundColor="Red" />
  <Switch x:Name="CompletedJobsSwitch" BackgroundColor="Yellow" Grid.Column="1" />
</Grid>

这是一个众所周知的问题,它与
堆栈布局
标签
相结合:


您可以使用中描述的变通方法。在这种情况下,关键是使用
网格
而不是
堆栈布局

也许我参加聚会太晚了,有些bug已经修复,但对于通过搜索到达这里的其他bug:

对于左侧项目,请使用HorizontalOptions=“StartAndExpand”
对于右对齐项,请使用HorizontalOptions=“End”
开关是一个固定长度的组件,因此您不想使用expand。如果使用EndAndExpand,它会在右侧添加一些填充,破坏您的正确对齐方式。
标签要填充剩余的空间,请告诉它展开

 <StackLayout VerticalOptions="Start" Orientation="Horizontal" Margin="0,10,0,10" BackgroundColor="Green" Padding="5,5,5,5">
            <Label Text="SHOW COMPLETED TASKS" BackgroundColor="Red"  HorizontalOptions="StartAndExpand"  />
            <Switch x:Name="CompletedJobsSwitch"  HorizontalOptions="End" IsToggled="false" BackgroundColor="Yellow"/>
</StackLayout>


这并不矛盾。
StackLayout
只占用所需的空间。试着在它周围的最高级别放置一个
网格
。当我说“不恒定”时,我指的是Xamarin表单的XAML,但是我已经将StackLayout设置为填充和扩展,为什么它不这样做呢?添加网格似乎是在解决这个问题。你不必添加网格,只要用网格替换顶级StackLayout即可。这个解决方案对我也很有效。我在使用Orientation=“Horizontal”的StackLayout时遇到了非常类似的问题,最后一项是使用HorizontalOptions=“End”设置的开关。它在所有尺寸的Android屏幕和更宽的IOS屏幕上工作正常(将开关对准屏幕右侧),但在较小的IOS屏幕上,开关向左侧滑动,如图所示。上面的网格方法在我测试过的所有屏幕上都运行良好。