【问题标题】:Grid content priority in Xamarin FormsXamarin Forms 中的网格内容优先级
【发布时间】:2020-05-15 10:53:18
【问题描述】:

我正在尝试使用 Xamarin.Forms 中的 Grid 制作类似钢琴的布局。看起来不错,但由于当我按下其中一个白色按钮时使用了 RowSpan,它会破坏附近黑色按钮的可见性。 Example pressing button "D" 我试图通过在按下白色按钮时将注意力集中在“损坏的”黑色按钮上来解决这个问题,但它没有解决任何问题。我会很感激任何提示。 这是我的 XAML 的一部分:

<Grid.Resources>
            <Style TargetType="Button">
                <Setter Property="BackgroundColor" Value="White"/>
                <Setter Property="Grid.RowSpan" Value="3"/>
                <Setter Property="Grid.Row" Value="0"/>
                <Setter Property="Grid.ColumnSpan" Value="3"/>
            </Style>
        </Grid.Resources>
        <!--white buttons-->
        <Button x:Name="C" Text="C" Grid.Column="0" Clicked="Button_Clicked"/>
        <Button x:Name="D" Text="D" Grid.Column="3" Clicked="Button_Clicked"/>
        <Button x:Name="E" Text="E" Grid.Column="6" Clicked="Button_Clicked"/>
        <Button x:Name="F" Text="F" Grid.Column="9" Clicked="Button_Clicked"/>
        <Button x:Name="G" Text="G" Grid.Column="12" Clicked="Button_Clicked"/>
        <Button x:Name="A" Text="A" Grid.Column="15" Clicked="Button_Clicked"/>
        <Button x:Name="H" Text="H" Grid.Column="18" Clicked="Button_Clicked"/>
        <!--black buttons-->
        <Button x:Name="Cis" Text="Cis" BackgroundColor="Black" TextColor="White" Grid.Row="0" Grid.Column="2" Grid.ColumnSpan="2" Grid.RowSpan="1" Clicked="Button_Clicked"/>
        <Button x:Name="Dis" Text="Dis" BackgroundColor="Black" TextColor="White" Grid.Row="0" Grid.Column="5" Grid.ColumnSpan="2" Grid.RowSpan="1" Clicked="Button_Clicked"/>
        <Button x:Name="Fis" Text="Fis" BackgroundColor="Black" TextColor="White" Grid.Row="0" Grid.Column="11" Grid.ColumnSpan="2" Grid.RowSpan="1" Clicked="Button_Clicked"/>
        <Button x:Name="Gis" Text="Gis" BackgroundColor="Black" TextColor="White" Grid.Row="0" Grid.Column="14" Grid.ColumnSpan="2" Grid.RowSpan="1" Clicked="Button_Clicked"/>
        <Button x:Name="Ais" Text="Ais" BackgroundColor="Black" TextColor="White" Grid.Row="0" Grid.Column="17" Grid.ColumnSpan="2" Grid.RowSpan="1" Clicked="Button_Clicked"/>

【问题讨论】:

  • 我想我会尝试将黑白键分成单独的网格
  • 试过了,但是在同一个地方有两个完全相同的网格,我只能弹白键或黑键(取决于后面写的内容)
  • 将它们彼此相邻对齐,而不是彼此重叠

标签: xamarin.forms


【解决方案1】:

这是由Xamarin.Forms Fast Renderers引起的:

Xamarin.Forms 4.0 开始,所有以FormsAppCompatActivity 为目标的应用程序将默认使用这些fast renderers

解决此问题的最快方法是在调用Forms.Init 覆盖fast renderers 之前将以下代码行添加到您的MainActivity 类中:

Forms.SetFlags("UseLegacyRenderers");

您还可以编写 Button 的自定义渲染器来改善行为。

我上传了一个示例项目here,你可以查看一下。

【讨论】:

    猜你喜欢
    • 2023-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 2021-08-28
    • 1970-01-01
    相关资源
    最近更新 更多