-1

I have this code:

using System;
using Xamarin.Forms;

namespace Test
{
    public partial class ScrollHeadingView : ContentPage
    {
        public ScrollHeadingView()
        {

            // more code here

            if (RightIconVisible)
            {
                // do some actions to add elements to each other here
                var rightIconPageHeadingSvg = new PageHeadingSvg() { HorizontalOptions = LayoutOptions.StartAndExpand }
                .Bind(PageHeadingSvg.SourceProperty, nameof(RightIconSource), source: this);
                grid3.AddChild(rightIconPageHeadingSvg);
                grid2.GestureRecognizers.Add(rightIconTap);
                grid3.AddChild(rightIconPageHeadingSvg);
            }

    }
}

and

public partial class DecksTabPage : ScrollHeadingView
{
    public DecksTabViewModel vm;
    public DecksTabPage()
    {
        RightIconVisible = true;
        BindingContext = vm = new DecksTabViewModel();
        InitializeComponent();
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        vm.OnAppearing();
    }

}

I am setting RightIconVisible to true and want that to be reflected in ScrollHeadingView when it is constructed.

But it's not happening.

Is there a way that I can set this to happen with code in DecksTabPage?

Note that if I was using XAML I would have done this by binding to the IsVisible of a grid and then set the IsVisible to show or not show that code that has the elements. But as I am using C# I am not sure how to do this.

Alan2
  • 23,493
  • 79
  • 256
  • 450

1 Answers1

2

Constructor of parent class works before any code in constructor of child class.
To pass some value to parent class constructor, you should add a constructor parameter to it and call base constructor from child with needed value.
For example:

public partial class ScrollHeadingView : ContentPage
{
     public ScrollHeadingView(bool rightIconVisible = false)
     {
          RightIconVisible = rightIconVisible;
          if (RightIconVisible)
          {
              // do some actions to add elements to each other here
          }
     }
}

public partial class DecksTabPage : ScrollHeadingView
{
    public DecksTabViewModel vm;

    public DecksTabPage():base(true)
    {
        BindingContext = vm = new DecksTabViewModel();
        InitializeComponent();
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        vm.OnAppearing();
    }
}

Other possibility is to remove code working with RightIconVisible from parent class constructor and put it into set of corresponding property.

public partial class ScrollHeadingView : ContentPage
{
     private bool rightIconVisible;

     public ScrollHeadingView()
     {}

     protected bool RightIconVisible
     {
        get => rightIconVisible;
        set 
        {
          rightIconVisible = value;
          if (rightIconVisible)
          {
              // do some actions to add elements to each other here
          }
     }
}

public partial class DecksTabPage : ScrollHeadingView
{
    public DecksTabViewModel vm;

    public DecksTabPage()
    {
        RightIconVisible = true;
        BindingContext = vm = new DecksTabViewModel();
        InitializeComponent();
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        vm.OnAppearing();
    }
}
Quercus
  • 2,015
  • 1
  • 12
  • 18
  • Would it also be possible to call a method in the base class? If so could you give that as an alternate way of doing it. For example could that same code block in the base be called from within the OnAppearing in DecksTabPage? – Alan2 Dec 03 '20 at 10:01
  • Note that while "before any code in the child's class constructor" is correct reality is a bit more nuanced - https://stackoverflow.com/questions/1882692/c-sharp-constructor-execution-order... and there are virtual methods that are called from derived class too... – Alexei Levenkov Dec 03 '20 at 10:03
  • @Alan2 Yes you can call method from child. However, even better maybe to create `set` handler for `RightIconVisible` property. Updated my answer – Quercus Dec 03 '20 at 10:13