Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Remove RadioButton.ButtonSource API #9967

Merged
merged 1 commit into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,23 @@ protected override void Build(StackLayout stackLayout)

var isCheckedContainer = new ValueViewContainer<RadioButton>(Test.RadioButton.IsChecked, new RadioButton() { IsChecked = true, HorizontalOptions = LayoutOptions.Start }, "IsChecked", value => value.ToString());

var checkedVisualState = new VisualState { Name = "IsChecked" };
checkedVisualState.Setters.Add(new Setter { Property = RadioButton.ButtonSourceProperty, Value = "rb_checked" });
//var checkedVisualState = new VisualState { Name = "IsChecked" };
//checkedVisualState.Setters.Add(new Setter { Property = RadioButton.ButtonSourceProperty, Value = "rb_checked" });

var group = new VisualStateGroup();
group.States.Add(checkedVisualState);
//var group = new VisualStateGroup();
//group.States.Add(checkedVisualState);

var normalVisualState = new VisualState{ Name = "Normal" };
normalVisualState.Setters.Add(new Setter { Property = RadioButton.ButtonSourceProperty, Value = "rb_unchecked" });
group.States.Add(normalVisualState);
//var normalVisualState = new VisualState{ Name = "Normal" };
//normalVisualState.Setters.Add(new Setter { Property = RadioButton.ButtonSourceProperty, Value = "rb_unchecked" });
//group.States.Add(normalVisualState);

var groupList = new VisualStateGroupList();
groupList.Add(group);
//var groupList = new VisualStateGroupList();
//groupList.Add(group);

var rbStateManaged = new RadioButton() { HorizontalOptions = LayoutOptions.Start };
VisualStateManager.SetVisualStateGroups(rbStateManaged, groupList);
//var rbStateManaged = new RadioButton() { HorizontalOptions = LayoutOptions.Start };
//VisualStateManager.SetVisualStateGroups(rbStateManaged, groupList);

var stateManagedContainer = new ValueViewContainer<RadioButton>(Test.RadioButton.ButtonSource, rbStateManaged, "IsChecked", value => value.ToString());
//var stateManagedContainer = new ValueViewContainer<RadioButton>(Test.RadioButton.ButtonSource, rbStateManaged, "IsChecked", value => value.ToString());

Add(borderButtonContainer);
Add(borderRadiusContainer);
Expand All @@ -134,7 +134,7 @@ protected override void Build(StackLayout stackLayout)
Add(textColorContainer);
Add(paddingContainer);
Add(isCheckedContainer);
Add(stateManagedContainer);
//Add(stateManagedContainer);
}
}
}
16 changes: 9 additions & 7 deletions Xamarin.Forms.Core/RadioButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ public class RadioButton : Button, IElementConfiguration<RadioButton>
public static readonly BindableProperty GroupNameProperty = BindableProperty.Create(
nameof(GroupName), typeof(string), typeof(RadioButton), null, propertyChanged: (b, o, n) => ((RadioButton)b).OnGroupNamePropertyChanged((string)o, (string)n));

public static readonly BindableProperty ButtonSourceProperty = BindableProperty.Create(
nameof(ButtonSource), typeof(ImageSource), typeof(RadioButton), null);
// TODO Needs implementations beyond Android
//public static readonly BindableProperty ButtonSourceProperty = BindableProperty.Create(
// nameof(ButtonSource), typeof(ImageSource), typeof(RadioButton), null);

public event EventHandler<CheckedChangedEventArgs> CheckedChanged;

Expand All @@ -37,11 +38,12 @@ public string GroupName
set { SetValue(GroupNameProperty, value); }
}

public ImageSource ButtonSource
{
get { return (ImageSource)GetValue(ButtonSourceProperty); }
set { SetValue(ButtonSourceProperty, value); }
}
// TODO Needs implementations beyond Android
//public ImageSource ButtonSource
Copy link
Contributor

@PureWeen PureWeen Mar 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens right now if people use ImageSource?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s kind of a funny API anyway. You’re able to set a custom image for the checkbox, but you need it together with the VSM to dynamically set the checked state. See the example in the gallery I commented out.

If people use it on platforms where it’s not implemented, nothing happens right now.

//{
// get { return (ImageSource)GetValue(ButtonSourceProperty); }
// set { SetValue(ButtonSourceProperty, value); }
//}

public RadioButton()
{
Expand Down
55 changes: 28 additions & 27 deletions Xamarin.Forms.Platform.Android/AppCompat/RadioButtonRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ protected virtual void OnElementChanged(ElementChangedEventArgs<Button> e)
UpdateInputTransparent();
UpdateBackgroundColor();
_buttonLayoutManager?.Update();
UpdateButtonImage(true);
//UpdateButtonImage(true);
UpdateIsChecked();
ElevationHelper.SetElevation(this, e.NewElement);
}
Expand All @@ -237,10 +237,10 @@ protected virtual void OnElementPropertyChanged(object sender, PropertyChangedEv
{
UpdateIsChecked();
}
else if (e.PropertyName == RadioButton.ButtonSourceProperty.PropertyName)
{
UpdateButtonImage(false);
}
//else if (e.PropertyName == RadioButton.ButtonSourceProperty.PropertyName)
//{
// UpdateButtonImage(false);
//}

ElementPropertyChanged?.Invoke(this, e);
}
Expand Down Expand Up @@ -338,28 +338,29 @@ void UpdateTextColor()
_textColorSwitcher.Value.UpdateTextColor(this, Button.TextColor);
}

void UpdateButtonImage(bool isInitializing)
{
if (Element == null || _isDisposed)
return;

ImageSource buttonSource = ((RadioButton)Element).ButtonSource;
if (buttonSource != null && !buttonSource.IsEmpty)
{
Drawable currButtonImage = Control.ButtonDrawable;

this.ApplyDrawableAsync(RadioButton.ButtonSourceProperty, Context, image =>
{
if (image == currButtonImage)
return;
Control.SetButtonDrawable(image);

Element.InvalidateMeasureNonVirtual(InvalidationTrigger.MeasureChanged);
});
}
else if(!isInitializing)
Control.SetButtonDrawable(null);
}
// TODO Needs implementations beyond Android
//void UpdateButtonImage(bool isInitializing)
//{
// if (Element == null || _isDisposed)
// return;

// ImageSource buttonSource = ((RadioButton)Element).ButtonSource;
// if (buttonSource != null && !buttonSource.IsEmpty)
// {
// Drawable currButtonImage = Control.ButtonDrawable;

// this.ApplyDrawableAsync(RadioButton.ButtonSourceProperty, Context, image =>
// {
// if (image == currButtonImage)
// return;
// Control.SetButtonDrawable(image);

// Element.InvalidateMeasureNonVirtual(InvalidationTrigger.MeasureChanged);
// });
// }
// else if(!isInitializing)
// Control.SetButtonDrawable(null);
//}

void UpdateIsChecked()
{
Expand Down