Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binding doesn't work #159

Open
karak opened this issue Jun 26, 2019 · 3 comments
Open

Binding doesn't work #159

karak opened this issue Jun 26, 2019 · 3 comments

Comments

@karak
Copy link

karak commented Jun 26, 2019

Binding Text property of IconButton doesn't work as expected(on iOS).

I created the following XAML, and dynamically changed the value to "fas-stop" from "fas-play", but saw "f...stop-icon" instead of "stop-icon".

I confirmed the text is right, and that a button initially set "fas-stop" works fine to conclude the binding mechanism of the component is simply broken.

<iconize:IconButton Text="{Binding PlayPauseButtonIcon}" Style="{StaticResource CommandbarButtonStyle}" Command="{Binding PlayPauseCommand}"/>

I tried to reproduce that bug by modifying your sample projects, only to fail to build just after checkout.

ADD: Application dependency:

  • Prism.Forms: 7.1.0.431
  • Xamarin.Forms: 3.6.0.264807
  • Iconize.Forms: 3.5.0.129
@charmosz
Copy link

Same for me. It works on Android but not on iOS.
Xamarin.Forms 4.0.0497661
Iconize.Forms 3.5.0.129

@karak karak changed the title Binding doen't work Binding doesn't work Jun 26, 2019
@karak
Copy link
Author

karak commented Jun 27, 2019

I found some workaround.
I removed "fas-" prefix when updating, and then this bugs magically disappeared.

Results:

  • fas-play -> fas-pause | (icon) -> fas-(icon)
  • fas-play -> pause | (icon) -> (icon)
  • play -> pause | play -> pause

@karak
Copy link
Author

karak commented Aug 30, 2019

My solution is to use some attached property.

Creating this property class:

    public class FasIcon
    {
        private static readonly string SpecialInitialValue = DateTime.UtcNow.ToLongTimeString();
        private static bool isIOS => Device.RuntimePlatform != Device.iOS;
        private static bool IsInitial(object value) => object.ReferenceEquals(value, SpecialInitialValue);
        
        public static readonly BindableProperty IconProperty = BindableProperty.CreateAttached(
            "Icon", typeof(string), typeof(FasIcon), SpecialInitialValue,
            propertyChanged: OnPropertyChanged);

        private static void OnPropertyChanged(BindableObject bindable, object oldValue, object newValue)
        {
            if (isIOS || IsInitial(oldValue))
            {
                SetValue(bindable, newValue);
            }
            else
            {
                SetValue(bindable, RemovePrefix((string)newValue));
            }
        }

        private static void SetValue(BindableObject bindable, object value)
        {
            bindable.SetValue(IconButton.TextProperty, value);
        }

        private static string RemovePrefix(string value)
        {
            var pos = value.IndexOf('-');
            return pos > 0 ? value.Substring(pos + 1) : value;
        }

        public static string GetIcon(BindableObject element) => (string) element.GetValue(IconProperty);
        public static void SetIcon(BindableObject element, string value) => element.SetValue(IconProperty, value);
    }

Use it like this. I used it with triggers:

            <Style x:Key="PlayPauseButtonStyle" TargetType="iconize:IconButton" BasedOn="{StaticResource CommandbarButtonStyle}">
                <Style.Triggers>
                    <DataTrigger TargetType="iconize:IconButton" Binding="{Binding PlayPauseIconState}" Value="Pause">
                        <Setter Property="views:FasIcon.Icon" Value="fas-pause" />
                    </DataTrigger>
                    <DataTrigger TargetType="iconize:IconButton" Binding="{Binding PlayPauseIconState}" Value="Play">
                        <Setter Property="views:FasIcon.Icon" Value="fas-play" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants