如何在 Xamarin 表单中使用全宽矩形裁剪网格

问题描述

我正在尝试为 Xamarin 表单中的图像剪辑或创建圆角。一切都按预期工作,但我遇到了如何让网格矩形占据全宽的问题。现在宽度是静态的。

<Grid Grid.Row="2" HeightRequest="400" RowDeFinitions="300,100" Margin="10,0">
                            <sh:Shadows Grid.Row="0" Grid.RowSpan="2">
                                <sh:Shadows.Resources>
                                    <Style targettype="sh:Shadows">
                                        <Setter Property="IsClippedToBounds" Value="False"/>
                                        <Setter Property="CornerRadius" Value="10"/>
                                        <Setter Property="Shades">
                                            <Setter.Value>
                                                <sh:ShadeStack>
                                                    <sh:Shade BlurRadius="5" Offset="0,5" Color="{DynamicResource ThemeComplementary}" />
                                                </sh:ShadeStack>
                                            </Setter.Value>
                                        </Setter>
                                    </Style>
                                </sh:Shadows.Resources>
                                <Frame Grid.Row="0" Grid.RowSpan="2" Padding="0" CornerRadius="10" HasShadow="False" IsClippedToBounds="True"/>
                            </sh:Shadows>
                            <Grid Grid.Row="0" IsClippedToBounds="True">
                                <Image Source="{Binding DailySupplicationBackgroundImageLink,Converter={StaticResource StringToImageSourceConverter}}" 
                                       HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Aspect="AspectFill">
                                </Image>
                                <Grid.Clip>
                                    <RoundRectangleGeometry Rect="0,200,300" CornerRadius="10,10,0"/>
                                </Grid.Clip>
                            </Grid>
                            <Grid Grid.Row="1" IsClippedToBounds="True">
                                <Label Grid.Row="0" Margin="10,0" Text="Some Text here"/>
                            </Grid>
                        </Grid>

我希望只从设置在内部网格中的两侧对图像进行圆角处理,如下所示。

<Grid Grid.Row="0" IsClippedToBounds="True">
                                <Image Source="{Binding DailySupplicationBackgroundImageLink,0"/> // Set Fill Width
                                </Grid.Clip>
                            </Grid>

这工作正常,但 Rect 的静态宽度。我希望在下面实现。

我知道有像 Pancake 这样的包,但我似乎没有必要使用那个包。

Sample Image With radius corner from two sides only

解决方法

终于从后面的代码解决了。 我没有剪裁网格,而是设法剪裁了图像本身。 我仍然有兴趣了解如何在 XAML 中而不是代码中执行相同的操作。我在 Page SizeChanged 事件中添加了以下内容以处理方向。

private void MydayPage_SizeChanged(object sender,EventArgs e)
        {
            image.Clip = new Xamarin.Forms.Shapes.RoundRectangleGeometry(new CornerRadius(10,10,0),new Rectangle
            {
                Width = Width-20,Height = 300,X = 0,Y = 0
            });
        }