maui-linux/Xamarin.Forms.Platform.WinRT/FormsButton.cs

102 строки
2.3 KiB
C#
Исходник Обычный вид История

2016-03-22 23:02:25 +03:00
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
2016-03-22 23:02:25 +03:00
#if WINDOWS_UWP
using WContentPresenter = Windows.UI.Xaml.Controls.ContentPresenter;
2016-03-22 23:02:25 +03:00
namespace Xamarin.Forms.Platform.UWP
#else
namespace Xamarin.Forms.Platform.WinRT
#endif
{
public class FormsButton : Windows.UI.Xaml.Controls.Button
{
public static readonly DependencyProperty BorderRadiusProperty = DependencyProperty.Register(nameof(BorderRadius), typeof(int), typeof(FormsButton),
2016-03-22 23:02:25 +03:00
new PropertyMetadata(default(int), OnBorderRadiusChanged));
public static readonly DependencyProperty BackgroundColorProperty = DependencyProperty.Register(nameof(BackgroundColor), typeof(Brush), typeof(FormsButton),
new PropertyMetadata(default(Brush), OnBackgroundColorChanged));
#if WINDOWS_UWP
WContentPresenter _contentPresenter;
#else
2016-03-22 23:02:25 +03:00
Border _border;
#endif
public Brush BackgroundColor
{
get
{
return (Brush)GetValue(BackgroundColorProperty);
}
set
{
SetValue(BackgroundColorProperty, value);
}
}
2016-03-22 23:02:25 +03:00
public int BorderRadius
{
get
{
return (int)GetValue(BorderRadiusProperty);
}
set
{
SetValue(BorderRadiusProperty, value);
}
2016-03-22 23:02:25 +03:00
}
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
#if WINDOWS_UWP
_contentPresenter = GetTemplateChild("ContentPresenter") as WContentPresenter;
#else
2016-03-22 23:02:25 +03:00
_border = GetTemplateChild("Border") as Border;
#endif
UpdateBackgroundColor();
2016-03-22 23:02:25 +03:00
UpdateBorderRadius();
}
static void OnBackgroundColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((FormsButton)d).UpdateBackgroundColor();
}
2016-03-22 23:02:25 +03:00
static void OnBorderRadiusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((FormsButton)d).UpdateBorderRadius();
}
void UpdateBackgroundColor()
{
if (BackgroundColor == null)
BackgroundColor = Background;
#if WINDOWS_UWP
if (_contentPresenter != null)
_contentPresenter.Background = BackgroundColor;
#else
if (_border != null)
_border.Background = BackgroundColor;
#endif
Background = Color.Transparent.ToBrush();
}
2016-03-22 23:02:25 +03:00
void UpdateBorderRadius()
{
#if WINDOWS_UWP
if (_contentPresenter != null)
_contentPresenter.CornerRadius = new CornerRadius(BorderRadius);
#else
if (_border != null)
_border.CornerRadius = new CornerRadius(BorderRadius);
#endif
2016-03-22 23:02:25 +03:00
}
}
}