2017-10-30 21 views
5

Używam formantu Frame w projekcie współdzielonym Xamarin Forms. mam tylko kilka stylów:Ustawienie Frame.BackgroundColor traci zaokrąglone narożniki w formularzach Xamarin

<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> 

i prostą kontrolę ramka w stronę XAML:

 <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> 

i ja dostać somthing tak:

enter image description here

teraz, gdybym spróbuj zmienić kolor tła w czasie wykonywania:

CreditCardPaymentResultFrame.BackgroundColor = Color.FromHex("#ed3700"); 

kontrola rama traci okrągłości border:

enter image description here

Nie rozumiem tego zachowania, muszę zmienić kolor z powrotem, ale chciałbym zachować zaokrąglone krawędzie.

Dzięki każdemu, kto dał mi rękę

+0

Co Platforma używacie? –

+0

Witam. Używam Androida –

Odpowiedz

1

obliczu podobnego problemu na Androidzie, ale związane jest kolor obramowania. Aby to naprawić, stworzyłem nową kontrolę, odziedziczył go od Frame i wdrożony mechanizm renderujący dla niego, ale używane VisualElementRenderer<Frame> jako klasy bazowej zamiast 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; 
     } 
    } 
    } 
} 
Powiązane problemy