How can I create FlexLayout in OnAppearing method

I need to create FlexLayout based on content from server. I'm using method like this:

private static FlexLayout CreateFlexLayout(IEnumerable<Item> Items)    
{
     //create layout based on Items collection
}

And trying to call it like this:

    protected override void OnAppearing()
    {
        base.OnAppearing();
        if (!viewModel.Items.Any())
        {
            viewModel.LoadItemsCommand.Execute(null);
            //loading items from server
        }
        this.Content = CreateFlexLayout(viewModel.Items);
        //trying to update Page
    }

It's not working. Page is clean. Content from the server is different any time so I can't build interface in Xaml and bind Items, but I can't recognize how to update Page from Code Behind.

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    I created a basic demo to test the feature using the following code, it works well. Please check if the flexLayout contains children and whether the data has been loaded from server.

    protected override void OnAppearing()
    {
        base.OnAppearing();
    
        StackLayout layout = new StackLayout();
        layout.Children.Add(new Label { Text = "test for reloading page" });
    
        this.Content = layout;
    }
    

    You can add a breakpoint in CreateFlexLayout method or add a condition statement check the layout.

    protected override void OnAppearing()
    {
        base.OnAppearing();
        if (!viewModel.Items.Any())
        {
            viewModel.LoadItemsCommand.Execute(null);
            //loading items from server
        }
    
        FlexLayout layout = CreateFlexLayout(viewModel.Items);
    
        if (layout.Children.Count > 0)
        {
            this.Content = layout;
        }
    }
    
Sign In or Register to comment.