用自定義LabelRenderer
很難實現您的要求。因爲沒有這樣的界面來修改背景顏色和Radius
。但是,您可以通過自定義View
來完成此操作。然後在UWP客戶端項目中,您可以使用UserControl
來呈現所需的控件。
CustomNewLabelControl.cs
public class CustomNewLabelControl : View
{
public static readonly BindableProperty LabelTextProperty = BindableProperty.Create(
propertyName: "LabelText",
eturnType: typeof(string),
declaringType: typeof(CustomNewLabelControl),
defaultValue: default(string));
public string LabelText
{
get { return (string)GetValue(LabelTextProperty); }
set { SetValue(LabelTextProperty, value); }
}
public static readonly BindableProperty LabelRadiusProperty = BindableProperty.Create(
propertyName: "LabelRadius",
eturnType: typeof(double),
declaringType: typeof(CustomNewLabelControl),
defaultValue: default(double));
public double LabelRadius
{
get { return (double)GetValue(LabelRadiusProperty); }
set { SetValue(LabelRadiusProperty, value); }
}
public static readonly BindableProperty LabelBackgroundProperty = BindableProperty.Create(
propertyName: "LabelBackground",
eturnType: typeof(Color),
declaringType: typeof(CustomNewLabelControl),
defaultValue: default(Color));
public Color LabelBackground
{
get { return (Color)GetValue(LabelBackgroundProperty); }
set { SetValue(LabelBackgroundProperty, value); }
}
}
NewLabelControl.xaml.cs
public sealed partial class NewLabelControl : UserControl
{
public NewLabelControl()
{
this.InitializeComponent();
this.DataContext = this;
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(NewLabelControl), new PropertyMetadata(0));
public SolidColorBrush LabelBackground
{
get { return (SolidColorBrush)GetValue(LabelBackgroundProperty); }
set { SetValue(LabelBackgroundProperty, value); }
}
public static readonly DependencyProperty LabelBackgroundProperty =
DependencyProperty.Register("LabelBackground", typeof(SolidColorBrush), typeof(NewLabelControl), new PropertyMetadata(0));
public CornerRadius LabelRadius
{
get { return (CornerRadius)GetValue(LabelRadiusProperty); }
set { SetValue(LabelRadiusProperty, value); }
}
public static readonly DependencyProperty LabelRadiusProperty =
DependencyProperty.Register("LabelRadius", typeof(CornerRadius), typeof(NewLabelControl), new PropertyMetadata(0));
public SolidColorBrush LabelForeground
{
get { return (SolidColorBrush)GetValue(LabelForegroundProperty); }
set { SetValue(LabelForegroundProperty, value); }
}
public static readonly DependencyProperty LabelForegroundProperty =
DependencyProperty.Register("LabelForeground", typeof(SolidColorBrush), typeof(NewLabelControl), new PropertyMetadata(0));
}
NewLabelControl.xaml
<Grid>
<Border CornerRadius="{Binding LabelRadius}" Background="{Binding LabelBackground}">
<TextBlock Text="{Binding Text}" Foreground="{Binding LabelForeground }" />
</Border>
</Grid>
CustomNewLabelRanderer.cs
internal class CustomNewLabelRanderer : ViewRenderer<CustomNewLabelControl, NewLabelControl>
{
protected override void OnElementChanged(ElementChangedEventArgs<CustomNewLabelControl> e)
{
base.OnElementChanged(e);
if (Control == null)
{
SetNativeControl(new NewLabelControl());
}
if (e.OldElement != null)
{
}
if (e.NewElement != null)
{
Control.Text = Element.LabelText;
Control.LabelRadius = new Windows.UI.Xaml.CornerRadius(Element.LabelRadius);
Color color = Element.LabelBackground;
Control.LabelBackground = new Windows.UI.Xaml.Media.SolidColorBrush(Windows.UI.Color.FromArgb((byte)(color.A * 255),
(byte)(color.R * 255),
(byte)(color.G * 255),
(byte)(color.B * 255)));
}
}
}
使用
<local:CustomNewLabelControl LabelText="Welcome to Xamarin Forms!" LabelBackground="Gray" LabelRadius="5"
VerticalOptions="Center"
HorizontalOptions="Center" />