locked
Bug : Switching IsVisible on a button does not redraw the button RRS feed

  • Question

  • User58997 posted

    Hi,

    I have found something weird, when switching IsVisibility of several button embbedded in a stacklayout, using a viewModel (Commands and bindings) the buttons does not always redraws themself (on iOS, did not try on Android).

    I have tree buttons, if the first is visible the others ar hidden, if I click on the first one, it desepears and the other are shown, if I click on the third one the two last button should despear and the first one should be visible.

    Here is my code (Test project attchaed) :

    public class App : Application
    {
        Button Stop;
        Button Record;
        Button Pause;
        MyViewModel vm;
        public App()
        {
            this.BindingContext = new MyViewModel();
            vm = (MyViewModel)this.BindingContext;
              Record = new Button
            {
                Text = "Record",
            };
              Record.SetBinding<MyViewModel>(Button.IsVisibleProperty, t => t.IsRecording, converter: new InvertBoolenConverter());
             Record.Command = vm.StartRecordingCommand;
    
            Pause = new Button
            {
                Text = "Pause",
            };
            Pause.SetBinding<MyViewModel>(Button.IsVisibleProperty, t => t.IsRecording);
    
            Stop = new Button
            {
                Text = "Stop",
            };
            Stop.SetBinding<MyViewModel>(Button.IsVisibleProperty, t => t.IsRecording);
            Stop.Command = vm.StopRecordingCommand;
    
            StackLayout stack = new StackLayout
            {
                Padding = new Thickness(10,10,10,10),
                BackgroundColor = Color.Red,
                Orientation = StackOrientation.Horizontal,
                VerticalOptions = LayoutOptions.Center,
                Children = {
                    Record, 
                    Pause,
                    Stop
                }
            };
    
            // The root page of your application
            MainPage = new ContentPage
            {
    
                Content=new StackLayout
                {
                    Children = {
                        stack,
    
                    }
                }
            };
        }
    

    The View Model :

    public class MyViewModel : ObservableObject
    {
    
        private bool isRecording;
    
        public bool IsRecording
        {
            get
            {
                return isRecording;
            }
            private set
            {
                isRecording = value;
    
                    this.NotifyPropertyChanged(() => IsRecording);
    
            }
    
        }
        public ICommand StartRecordingCommand
        {
            get
            {
                return new Command(() =>
                {
                    IsRecording = true;
                });
            }
        }
    
        public ICommand StopRecordingCommand
        {
            get
            {
                return new Command(() =>
                {
                    IsRecording = false;
                });
            }
        }
    }
    
    Monday, April 13, 2015 10:01 AM

All replies

  • User181 posted

    I didn't see any issues when running your code on iOS. Could you be more specific?

    Monday, April 13, 2015 6:59 PM
  • User58997 posted

    Hi Admam, Here are some screenshots : 1 - When the app start only record is visible (ok)

    2- When I click on record Pause et Start shows up and Record is hidden (ok)

    3- When I click on stop I should go back to screenshot 1 but nothing shows up :

    Note that it does not fail systematically, sometimes, but if you do that several times in a rows the bug will shows up. I am wondering if this is not related to this : https://forums.xamarin.com/discussion/23327/multiple-isvisible-performance-question

    Tuesday, April 14, 2015 8:50 AM
  • User58997 posted

    Might be the same problem as http://forums.xamarin.com/discussion/35461/isvisible-inside-control-inside-stacklayout-inside-scrollview-not-work#latest

    Tuesday, April 14, 2015 3:47 PM
  • User181 posted

    I'm not sure what to tell you because I was never able to reproduce that behavior.

    Tuesday, April 14, 2015 7:10 PM
  • User58997 posted

    Did you tryed with the solution I provided in attachement ?

    Tuesday, April 14, 2015 7:16 PM
  • User181 posted

    Yes. I was finally able to reproduce it, actually, but it took a while. I don't know what's going on. Must be a bug. I suggest filing a bug report.

    Tuesday, April 14, 2015 7:18 PM
  • User58997 posted

    Hopefully you could reproduce ! Here is the bug : https://bugzilla.xamarin.com/show_bug.cgi?id=29086

    Tuesday, April 14, 2015 7:20 PM
  • User31385 posted

    @AxelCharpentier, @adamkemp I can confirm I've seen this also. It seems to relate to the PropertyChanged notification on Boolean values, in particular the IsVisible property of things like images and buttons. The only way I could solve the problem was to "manually" set the IsVisible property of the View object to the same value as its bound ViewModel object in an event handler inside the Page's code-behind file. The event handler was triggered in OnPropertyChanged for a different property that summarised the state of the Page (hence the IsVisible-ness of each affected control).

    ViewModel:

    public bool ShowThisControl { get; set; }

    View:

    <Button x:Name="myButton" Visible="{Binding ShowThisControl}" />

    Page's Code-behind event handler:

    if (MyPage.PageState == PageStates.AllControlsVisible) { myButton.IsVisible = ((MyViewModel)BindingContext).ShowThisControl; }

    This is obviously over-simplified but I think you'll get the idea.

    HTH

    Wednesday, April 15, 2015 4:23 AM
  • User5087 posted

    I have here the same problem with IsVisible. It happens not allways if you change IsVisble. Would say, 1 of 8 changes, but it happens. @TheRealJasonSmith said, that this is a race condition problem and he wants to solve it. But I didn't saw until now no solution for this :(

    Wednesday, April 15, 2015 6:05 AM
  • User118856 posted

    We have the same issue in our App and the users are able to create the problem in 20% of the cases. This is really annoying.

    Friday, April 17, 2015 3:05 PM
  • User58997 posted

    Don't hesitate to tell that you have the same issue on bugzilla : https://bugzilla.xamarin.com/show_bug.cgi?id=29086

    May be it will speed up the fix

    Tuesday, April 21, 2015 9:34 AM
  • User58997 posted

    It also happen when toggling visibilty on the whole StackLayout :( This is really a blocking bug, Hope it will be fixed quickly.

    Tuesday, April 21, 2015 3:16 PM
  • User73562 posted

    Any update regarding this bug? Any workaround?

    Monday, April 27, 2015 11:58 AM
  • User58997 posted

    It has been confirmed yesterday in Bugzilla, but the fix is still not planed, and the importance is "Normal", I'd have put "High" since it impacts a wide used feature, for now I considere the IsVisible property buged and it may impact all controls on iOS

    Tuesday, April 28, 2015 11:55 AM
  • User120194 posted

    As a workaround, try setting the HeightRequest property of the child control to 0 to hide it, and -1 to show it on iOS.

    Wednesday, May 13, 2015 12:47 AM
  • User79642 posted

    Seeing the same thing in a Grid Layout on iOS. I ended up working around it by using Opacity instead of IsVisible, but that's certainly not ideal.

    Making a little noise in this thread in the hope that the bug will get moved to IN_PROGRESS soon.

    Thursday, June 11, 2015 11:21 PM
  • User58997 posted

    @ElijaHart you are right, I think it is a bug with all controls, not particular when embedded in stacklayout...

    Friday, June 12, 2015 7:48 AM
  • User58997 posted

    Looks like this bug is fixed with the last release Xamarin.Forms 1.4.3-pre3 Released anybody can confirm ?

    Wednesday, June 17, 2015 9:17 PM
  • User42522 posted

    @AxelCharpentier Have you checked if it is solved at all? Now we have stable 1.4.4 version.

    In my case I am binding the IsVisibleProperty of a Button in a ListViewItemTemplate. I don't see any issue in iOS and Android. But it works inconsistent in Windows Phone 8.1 Silverlight app.

    Monday, August 17, 2015 3:08 PM
  • User58997 posted

    @ShantimohanElchuri For my own case it was solved with 1.4.3 (iOS and Android). I didnt try on windows phone since my app does not suppport this platform

    Saturday, August 22, 2015 5:41 PM