Как программно установить радиус угла для кнопки в xamarin uwp? - PullRequest
0 голосов
/ 10 ноября 2018

У меня есть сетка, удерживаемая в кнопке. Я хочу, чтобы программно установить радиус угла этой кнопки в xamarin.uwp. Если это возможно, значит, я хочу установить угловой радиус для нижнего левого края. Пожалуйста, предложите свои идеи для этого запроса.

1 Ответ

0 голосов
/ 12 ноября 2018

По вашему требованию вы можете на заказ BottomLeft BindableProperty. затем используйте его в вашей пользовательской кнопке рендеринга, как показано ниже.

Кнопка Custom Forms

public class MyButton : Button
{

    public static readonly BindableProperty BottomLeftProperty = BindableProperty.Create(
      propertyName: "BottomLeft",
      returnType: typeof(int),
      declaringType: typeof(MyButton),
      defaultValue: default(int));

    public int BottomLeft
    {
        get { return (int)GetValue(BottomLeftProperty); }
        set { SetValue(BottomLeftProperty, value); }
    }
}

CustomButtonRenderer.cs

public class CustomButtonRenderer : ButtonRenderer
{
    Windows.UI.Xaml.Controls.ContentPresenter _contentPresenter;
    MyButton _myElement;
    protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
    {
        base.OnElementChanged(e);
        if (Control == null)
        {
            SetNativeControl(new FormsButton());
        }
        if (e.NewElement != null)
        {
            Control.Loaded += Control_Loaded;
            _myElement = Element as MyButton;
        }
    }

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

        if (e.PropertyName == MyButton.BottomLeftProperty.PropertyName)
        {
            UpdateBottomLeftBorderRadius(_myElement.BottomLeft);
        }
    }

    private void Control_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {
        _contentPresenter = MyFindChildByName(Control, "ContentPresenter") as Windows.UI.Xaml.Controls.ContentPresenter;

        if (_myElement.IsSet(MyButton.BottomLeftProperty) && _myElement.BottomLeft != (int)MyButton.BottomLeftProperty.DefaultValue)
        {
            UpdateBottomLeftBorderRadius(_myElement.BottomLeft);
        }
    }

    private void UpdateBottomLeftBorderRadius(int bottomLeft)
    {
        if (_contentPresenter != null)
        {
            _contentPresenter.CornerRadius = new CornerRadius(0, 0, 0, bottomLeft);
        }

    }

    public static DependencyObject MyFindChildByName(DependencyObject parant, string ControlName)
    {
        int count = VisualTreeHelper.GetChildrenCount(parant);

        for (int i = 0; i < count; i++)
        {
            var MyChild = VisualTreeHelper.GetChild(parant, i);
            if (MyChild is FrameworkElement && ((FrameworkElement)MyChild).Name == ControlName)
                return MyChild;

            var FindResult = MyFindChildByName(MyChild, ControlName);
            if (FindResult != null)
                return FindResult;
        }
        return null;
    }

}

Использование

<local:MyButton x:Name="Hello" Text="hello" 
                WidthRequest="100" 
                HeightRequest="100"  
                Margin="0,100,0,0" 
                BottomLeft="15" 
                VerticalOptions="Center" 
                HorizontalOptions="Center" 
                Clicked="MyButton_Clicked"/>

enter image description here

Для программного редактирования нижнего левого свойства.

HelloBtn.BottomLeft = 30;
...