Xamarin形式:框架阴影自定义渲染器

时间:2018-10-23 18:15:52

标签: c# ios xaml xamarin.forms xamarin.ios

我有一个自定义渲染器,可以更改安装阴影的图层的属性,但是,当我需要更改其大小时,在屏幕上显示的该帧不会“合并”阴影。阴影保留在我之前的位置,我使用了自定义渲染器,因为本机阴影非常“强”,并且我还没有找到更改它的方法。

任何关于缺少什么的建议都是正常的?我已经尝试过将它带到更改的框架的属性上,并让它再次执行“绘制”,但是并没有解决。

自定义渲染器:

public override void Draw(CGRect rect)
        {
            base.Draw(rect);
            Layer.ShadowRadius = 3;
            Layer.ShouldRasterize = true;
            Layer.ShadowColor = UIColor.Gray.CGColor;
            Layer.ShadowOffset = new CGSize(0, 1);
            Layer.ShadowOpacity = 0.25f;
            Layer.ShadowPath = UIBezierPath.FromRoundedRect(Layer.Bounds, Element.CornerRadius).CGPath;
            Layer.MasksToBounds = false;
        }

Cardfarme xaml:

<?xml version="1.0" encoding="utf-8" ?>
<customControls:CardFrame
    x:Class="dragon.Views.UserControls.CardListUserControl"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:b="clr-namespace:Prism.Behaviors;assembly=Prism.Forms"
    xmlns:customControls="clr-namespace:dragon.CustomControls;assembly=dragon"
    HasShadow="True"
    MinimumHeightRequest="160">

    <Frame
        x:Name="ColoredFrame"
        CornerRadius="3"
        HasShadow="False">
        <Frame Style="{StaticResource CardWhiteFrame}">
            <StackLayout
                x:Name="Teste"
                HorizontalOptions="Fill"
                Spacing="10"
                Style="{StaticResource CardContentStacklayoutStyle}">
                <StackLayout HorizontalOptions="Fill" Orientation="Horizontal">
                    <Label
                        x:Name="HeaderLabel"
                        Style="{StaticResource CardHeaderTextStyle}"
                        Text="{Binding HeaderText}" />
                    <Image
                        x:Name="ImgAbout"
                        Margin="0,5,0,15"
                        HorizontalOptions="EndAndExpand"
                        Source="about1.png" />
                </StackLayout>

                <ListView
                    x:Name="CardItemsList"
                    ItemsSource="{Binding ItemsList}"
                    SeparatorColor="LightGray"
                    VerticalOptions="StartAndExpand">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <Grid Margin="5,0,10,0">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="Auto" />
                                    </Grid.ColumnDefinitions>
                                    <Label
                                        Grid.Column="0"
                                        HorizontalOptions="Start"
                                        LineBreakMode="TailTruncation"
                                        Text="{Binding .}"
                                        TextColor="#333333"
                                        VerticalOptions="Center" />
                                    <Grid
                                        Grid.Column="1"
                                        HorizontalOptions="FillAndExpand"
                                        VerticalOptions="FillAndExpand">
                                        <Grid.GestureRecognizers>
                                            <TapGestureRecognizer Command="{Binding Path=BindingContext.RemoveCommand, Source={x:Reference CardItemsList}}" CommandParameter="{Binding .}" />
                                        </Grid.GestureRecognizers>
                                        <BoxView
                                            x:Name="RemoveBoxView"
                                            Grid.Column="1"
                                            CornerRadius="2"
                                            HeightRequest="3"
                                            HorizontalOptions="Center"
                                            VerticalOptions="Center"
                                            WidthRequest="30"
                                            Color="{StaticResource DefaultRedColor}" />
                                    </Grid>
                                </Grid>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
                <Entry
                    x:Name="EntryOfNewItemToInsert"
                    IsVisible="False"
                    Placeholder="Inserir novo item"
                    Text="{Binding NewItemToAdd}"
                    Unfocused="EntryOfNewItemToInsert_Unfocused">
                    <Entry.Behaviors>
                        <b:EventToCommandBehavior Command="{Binding InserNewItemCommand}" EventName="Unfocused" />
                    </Entry.Behaviors>
                </Entry>
                <Button
                    x:Name="Button"
                    Clicked="Button_Clicked"
                    Style="{StaticResource DefaultOrangeButtonStyle}"
                    Text="{Binding ButtonText}" />
            </StackLayout>
        </Frame>
    </Frame>
</customControls:CardFrame>

打印: enter image description here

在图像中,我们有第一张卡片,该卡片具有列表内的项目,因此可以进行扩展;而在另一张卡片中,列表中没有更改(无需扩展)的卡应该是正确的,请记住,如果原生阴影有效,而且Android也能正常工作,因为它没有应用自定义渲染器。

1 个答案:

答案 0 :(得分:0)

像这样覆盖OnElementPropertyChanged:

protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
     var rect = new CGRect(0, 0, 0, 0);
     SetNeedsDisplay();
}