Weird issue after forms update from 3.6 to 4.2 : IsVisible doesn't work after adding children

Hi everyone,
I updated recently a project from XF 3.6 to 4.2 and I have a very strange issue.
I have a "popup" hidden stacklayout that I change to visible. The weird thing is that, when I set the IsVisible property to true after adding a children, it seems that the layout doesn't handle it. No error, Console.Write(popup.IsVisible) says it's "true", tracing Children of the layout is perfectly ok, but the StackLayout is still invisible.

The real weird part is that when I set the IsVisible property before adding a child to the popup StackLayout, it perfectly seems to work.

Before the XF package update it uses to work no matter changing the property was before or after adding children.

Has anyone the same issue, and knows why?

thanks for your help!

Tagged:

Answers

  • JohnHardmanJohnHardman GBUniversity mod

    @JoLapraline

    I suggest posting your code in case somebody can spot a problem

  • JoLapralineJoLapraline FRMember ✭✭
    edited September 20

    Hi John,
    Actually I had it wrong diagnosis...Actually the StackLayout IS visible...It is his programmaticaly added child that is not visible.
    I added a TapGestureRecognizer over the Stacklayout and a red background. So the Stack is shown, but the child I just added in it is not visible. here is sample code. the behaviour happen even on a fresh new project:

    Mainpage

    Code behind:
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
    public MainPage()
    {
    InitializeComponent();
    _btn.Clicked += (sender, e) =>
    {

                _popup.Children.Clear();
                _popup.Children.Add(new SubV());
    
                _popup.IsVisible = true;
    
    
    
            };
    
            _popup.Children.Clear();
            _popup.Children.Add(new SubV());
    
            _popup.IsVisible = true;
    
            _popup.GestureRecognizers.Add(new TapGestureRecognizer()
            {
                Command = new Command(() =>
                {
                    Console.WriteLine("click");
                })
            });
        }
    }
    

    the Custom View I'm adding to _popup (last StackLayout inside AbsoluteLayout in MainPage.xaml):
    SubV

    code-behind:
    public partial class SubV : StackLayout
    {
    public SubV()
    {
    InitializeComponent();
    _btn.Clicked += (sender, e) =>
    {
    ((StackLayout)this.Parent).IsVisible = false;
    ((StackLayout)this.Parent).Children.Clear();
    };
    }
    }

  • JoLapralineJoLapraline FRMember ✭✭

    I forgot to mention : this behaviour happens after I already added and cleared the popuplayout at startup. like you can see on the code behind of MainPage. The first time , the content of popup children is ok.

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited September 20

    Please post a screen or gif to display the issue. I've reproduced your code, it seems works well and I can't get what issue you mean.
    Besides, why add _popup.Children.Clear() code before adding content to layout. You have achieved this feature on custom layout.

    _btn.Clicked += (sender, e) =>
    {
        //_popup.Children.Clear();
        _popup.Children.Add(new CustomLayout());
        _popup.Children.Add(new Label { Text="label"});
    
        _popup.IsVisible = true;
    };
    

  • JoLapralineJoLapraline FRMember ✭✭

    Hi Jarvan!
    Did you tried exactly like me? I believe you tried on a StackLayout as your Page content. My MainPage content is an AbsoluteLayout. Let me explain :
    In my mainpage's content there is an AbsolutLayout containing 2 StackLayout (the 2nd one is overlaying the first, but is not visible at startup. The MainPage start and adds right after the SubView (SubV) in _popup Children. I click on the Subview, and it disapear as I want. Everything is ok for now. The misbehavior starts when I click on the button that adds again the Subview. Maybe it because the hole content is on an AbsoluteLayout...

    MainPage.xaml:
    ContentPage
    AbsoluteLayout
    StackLayout AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1"
    Button x:Name="_btn" Text="Welcome to Xamarin.Forms!"
    StackLayout
    StackLayout AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1" IsVisible="false" x:Name="_popup"
    AbsoluteLayout
    ContentPage

    code behind:
    public MainPage()
    {
    InitializeComponent();
    _btn.Clicked += (sender, e) =>
    {
    _popup.Children.Clear();
    _popup.Children.Add(new SubV());
    _popup.IsVisible = true;
    };

    _popup.Children.Clear();
    _popup.Children.Add(new SubV());
    _popup.IsVisible = true;
    _popup.GestureRecognizers.Add(new TapGestureRecognizer()
    {
    Command = new Command(() =>
    {
    Console.WriteLine("click");
    })
    });
    }

    I have a custom view that I add to _popup:
    SubV.xaml
    StackLayout
    Button x:Name="_btn"
    StackLayout

    code behind:
    public SubV()
    {
    InitializeComponent();
    _btn.Clicked += (sender, e) =>
    {
    ((StackLayout)this.Parent).IsVisible = false;
    ((StackLayout)this.Parent).Children.Clear();
    };
    }

  • JoLapralineJoLapraline FRMember ✭✭

  • JarvanJarvan Member, Xamarin Team Xamurai

    Sorry for that, I think StackLayout won't affect the test. I've changed the code with AbsoluteLayout on Xamarin.Forms 3.6, it works as below.

  • JoLapralineJoLapraline FRMember ✭✭
    edited September 21

    hi Jarvan! thank you for your help :)
    I think I wasn't clear enough. I said that it works well on Forms 3.6 but not on Forms 4.2. And you don't use a custom content view. you add a label programmaticaly. this works for me too. the strange behavior happens when you create a forms content view (as I showed on my first post, I called it SubV.xaml/SubV.xaml.cs(code-behind). So :

    _popup.Children.Add(new Label { Text="label"});
    _popup.Children.IsVisible = true; //works

    _popup.Children.Add(new SubV());
    _popup.Children.IsVisible = true; //will once. but fails after the 2nd click

    _popup.Children.IsVisible = true; //will work...
    _popup.Children.Add(new SubV());

    Maybe you can look at my files if you have a little more time?

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited September 23

    I reproduced the code and faced the issue you mentioned. After moving _popup.Children.IsVisible = true; before layout.Children.Add(), it works well. It seems that you should ensure the layout available before and the logic may be updated in new version of Xamarin.Forms. I viewed the release notes of Xamarin.Forms, but not get a detailed description about that.

    And I tested the simple code about adding elements to a control as below. It doesn't work when you set the parent's IsVisible to false.

    <StackLayout x:Name="layout">
        <Button x:Name="_btn"
                Text="button"
                Clicked="Button_Clicked"
                HorizontalOptions="Center"
                />
    </StackLayout>
    
    private void Button_Clicked(object sender, EventArgs e)
    {
        layout.IsVisible = false;
        layout.Children.Add(new Label { Text = "content" });
        layout.IsVisible = true;
    }
    

    Besides, you can report a new issue about this problem on github.
    https://github.com/xamarin/Xamarin.Forms/issues/new/choose

Sign In or Register to comment.