设置Frame.BackgroundColor在Xamarin表单上丢失圆角

Gia*_*lis 6 c# xamarin.forms

我在Xamarin Forms共享项目上使用Frame控件.我只是有一些风格:

<Color x:Key="Info">#0060ac</Color>
...
<Style x:Key="LabelContainer" TargetType="Frame">
    <Setter Property="Padding" Value="5" />
    <Setter Property="HorizontalOptions" Value="Fill" />
</Style>
<Style x:Key="LabelContainer-Info" TargetType="Frame" BasedOn="{StaticResource LabelContainer}">
    <Setter Property="BackgroundColor" Value="{DynamicResource Info}" />
</Style>
Run Code Online (Sandbox Code Playgroud)

和XAML页面中的简单Frame控件:

        <Frame x:Name="CreditCardPaymentResultFrame" Style="{StaticResource LabelContainer-Info}" Padding="0">
            <Label x:Name="PaymentErrorLabel" Text="Lorem ipsum" IsVisible="True" 
                   HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" 
                   VerticalTextAlignment="Center" HorizontalTextAlignment="Center" 
                   FontSize="18" TextColor="White">
            </Label>
        </Frame>
Run Code Online (Sandbox Code Playgroud)

我得到这样的事情:

在此输入图像描述

现在,如果我尝试在运行时更改背景颜色:

CreditCardPaymentResultFrame.BackgroundColor = Color.FromHex("#ed3700");
Run Code Online (Sandbox Code Playgroud)

框架控件失去边框圆度:

在此输入图像描述

我不明白这种行为,我需要改变背面颜色,但我想保持圆形边缘.

感谢任何帮我的人

Yeh*_*kyi 2

Android 上也遇到类似的问题,但与边框的颜色有关。为了解决这个问题,我创建了一个新控件,继承它Frame并为其实现渲染器,但用作VisualElementRenderer<Frame>基类而不是FrameRenderer

[assembly: ExportRenderer(typeof(MyFrame), typeof(MyFrameRenderer))]
namespace Android.Renderers
{
  public class MyFrameRenderer : VisualElementRenderer<Frame>
  {
    protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
    {
        base.OnElementChanged(e);

        if (e.NewElement != null)
        {
            var drawable = new GradientDrawable();
            ViewGroup.SetBackground(drawable);
            UpdateBackgroundColor(drawable);
            UpdateCornerRadius(drawable);
            UpdateOutlineColor(drawable);
            UpdateShadow();
        }
    }

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);

        var drawable = ViewGroup?.Background as GradientDrawable;
        if (drawable != null)
        {
            if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName)
            {
                UpdateBackgroundColor(drawable);
            } 
            else if (e.PropertyName == Frame.CornerRadiusProperty.PropertyName)
            {
                UpdateCornerRadius(drawable);
            }
            else if (e.PropertyName == Frame.OutlineColorProperty.PropertyName)
            {
                UpdateOutlineColor(drawable);
            }
            else if (e.PropertyName == Frame.HasShadowProperty.PropertyName)
            {
                UpdateShadow();
            }
        }
    }

    protected override void UpdateBackgroundColor()
    {
        // This method doesn't work well in Xamarin.Forms -Version 2.3.4.247
    }

    private void UpdateCornerRadius(GradientDrawable drawable)
    {
        drawable.SetCornerRadius(Element.CornerRadius);
    }

    private void UpdateOutlineColor(GradientDrawable drawable)
    {
        drawable.SetStroke(1, Element.OutlineColor.ToAndroid());
    }

    private void UpdateBackgroundColor(GradientDrawable drawable)
    {
        drawable.SetColor(Element.BackgroundColor.ToAndroid());
    }

    private void UpdateShadow()
    {
        if (Element.HasShadow)
        {
            Elevation = (float)Context.FromPixels(10);
        }
        else
        {
            Elevation = 0;
        }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)