我需要将此标记转换为可重用的控件:
<Frame BackgroundColor = "{DynamicResource BaseColor}" Margin="50,2,50,2">
<Frame.CornerRadius>
<OnPlatform x:TypeArguments="x:Single" iOS="20" Android="25"/>
</Frame.CornerRadius>
<Controls:CustomLabel Text = "Login" HorizontalTextAlignment="Center"
TextColor="White"/>
<Frame.GestureRecognizers>
<TapGestureRecognizer Command = "{Binding LogIn}" CommandParameter="{Binding}" />
</Frame.GestureRecognizers>
</Frame>
初始描述:
public class CustomButton : ContentView
{
StackLayout parentStack;
public CustomButton()
{
#region Initializing control
parentStack = new StackLayout();
Label textLabel = new Label { BackgroundColor = BackgroundColor, TextColor = TextColor, Text = Text };
Frame frame = new Frame { BackgroundColor = BackgroundColor, CornerRadius = CornerRadius, Content = textLabel };
parentStack.Children.Add(frame);
Content = parentStack;
#endregion
}
public static readonly BindableProperty CornerRadiusProperty =
BindableProperty.Create(nameof(CornerRadius), typeof(float), typeof(CustomButton), defaultValue: default(float), propertyChanged:RadiusSet);
private static void RadiusSet(BindableObject bindable, object oldValue, object newValue)
{
var @this = (CustomButton)bindable;
@this.CornerRadius = (float)newValue;
}
public static readonly BindableProperty TextProperty =
BindableProperty.Create(nameof(Text), typeof(string), typeof(CustomButton), defaultValue: default(string), propertyChanged: TextSet);
private static void TextSet(BindableObject bindable, object oldValue, object newValue)
{
var @this = (CustomButton)bindable;
@this.Text = (string)newValue;
}
public static readonly BindableProperty TextColorProperty =
BindableProperty.Create(nameof(TextColor), typeof(Color), typeof(CustomButton), defaultValue: Color.Black, propertyChanged: TextColorSet);
private static void TextColorSet(BindableObject bindable, object oldValue, object newValue)
{
var @this = (CustomButton)bindable;
@this.TextColor = (Color)newValue;
}
public float CornerRadius
{
get { return (float)GetValue(CornerRadiusProperty); }
set { SetValue(CornerRadiusProperty, value); }
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public Color TextColor
{
get { return (Color)GetValue(TextColorProperty); }
set { SetValue(TextColorProperty, value); }
}
}
我所需要做的就是在解析控件时设置可绑定属性。但是显然构造函数不是正确的地方。我现在唯一想到的选择就是BP的propertyChanged
,但是当它们的重要性相同时,哪个负责呢?