MAUI custom control positioning on load

366 views Asked by At

I have Frame(with Entry) inside custom control

   ` <Frame Grid.Row="0" 
       Grid.Column="1"
       x:Name="entryFrame"
       ...>
    <Entry Text="{Binding Source={x:Reference this}, Path=ContactText}"
           x:Name="entryContact"
           ...>
        <Entry.GestureRecognizers>
            <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
        </Entry.GestureRecognizers>
    </Entry>
</Frame>`

Frame should change position and colour depending on some property.

   protected void SetIsAvailable(bool _isavailable)
    {
        if(_isavailable)
        {
            entryFrame.BackgroundColor = Colors.LightSeaGreen;
            entryFrame.TranslateTo(0, 0, 0, easing: Easing.Linear);
        }
        if (!_isavailable)
        {
            entryFrame.BackgroundColor = Colors.PaleVioletRed;
            entryFrame.TranslateTo(100, 0, 0, easing: Easing.Linear);
        }
    }

Property:

  private bool _isAvailble;
    public bool IsAvailble
    {
        get
        {
            return _isAvailble;
        }
        set 
        {               
            _isAvailble= value;
            SetIsAvailable(value);
        }
    }

So when property changes I calling SetIsAvailable(value); that moving and painting my frame. In action it works perfect, but.... on first load if I set property in XAML

<controls:ContactPicker 
     IsAvailble="False"
     ControlState="Label"
     Labeltext="Phone:" 
     ContactText="65669437254"/> 

    

It changes frame color entryFrame.BackgroundColor = Colors.PaleVioletRed;, but not changing frame position entryFrame.TranslateTo(100, 0, 0, easing: Easing.Linear); 0_o

According to debugger line is executed, but it doesn't affect Frame. I suppose, on initialization compiler first initializes controls - then sets up properties(that calls my method) - and only after setting up controls positions and overwrite my changes

The question is - how to force that piece of..... code works correctly and set position and color after full initialization but before showing it to user. Might be there is some base method that calling very last and could be overrided?

Before, I also tried to call SetIsAvailable method after InitializeComponent(); in the main constructor, but apparently, Properties set up after. So the method doesn't make any changes.

1

There are 1 answers

6
Ant T On

So apparently trouble is partly the Custom control(myControl) initialization process, partly frame by yourself.

Properties are set up after calling myControl main constructor, so changing myCOntrol design/positions depending on properties in the main constructor is a useless idea. But overriding the OnBindingContextChanged method with my SetIsAvailable() helped.

The next part is positioning. Frame conrol does't want to react on any positioning during initialization.... I guess Frame just wrapping himself around control inside(Entry in my case) and inheriting his position state. But if we wrap Entry or Frame by itself in a Grid and position the Grid, it works correctly.

I ain't 100% sure that my approach is totally correct in terms of "good coding" standards, but it works.... so I ain't gonna touch it:))