【问题标题】:How to set corner radius for button in xamarin uwp by programatically?如何以编程方式设置xamarin uwp中按钮的角半径?
【发布时间】:2019-04-13 17:08:51
【问题描述】:

我在 Button 中有一个网格。我想通过在 xamarin.uwp 中以编程方式设置此按钮的角半径。如果可能意味着我想单独为左下角设置角半径。请提出您对此查询的想法。

【问题讨论】:

  • 你只想设置左下角半径吗?
  • 是的,左下角和右下角@NicoZhu-MSFT
  • 嗨@KarthikRj 有用吗?

标签: xamarin xamarin.forms uwp xamarin.uwp


【解决方案1】:

根据您的要求,您可以自定义BottomLeft BindableProperty。然后在您的自定义按钮渲染中使用它,如下所示。

自定义表单按钮

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

用于以编程方式编辑左下角的属性。

HelloBtn.BottomLeft = 30;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 2014-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多