Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Frame Width/HeightRequest Change Repositions in AbsoluteLayout

bfosterbfoster Member ✭✭
edited March 7 in Xamarin.Forms

I am new to using Xamarin.Forms to develop an app for both Android and iOS. I have been having a problem for the past week or so that I can't wrap my head around.

Basically, I am implementing custom renderers for custom controls. The parent control inherits from AbsoluteLayout (a "Canvas" of sorts for the children) and the children are objects that inherit from Frame. I am trying to create the ability to have the user move and resize the custom Frame objects.

I have managed to get the objects to move and update their position inside the AbsoluteLayout when they are touched and dragged. Great! Now on to resizing the Frames. I understand that I cannot change the width or height directly, and have to make a call to Width/HeightRequest, which then basically asks the parent if it can update, and the rest of the layout cycle occurs for the AbsoluteLayout. When I try to invoke the following code in the custom renderer,

Element.WidthRequest += 1.0;

the position of the Frame always resets to (0,0) inside the AbsoluteLayout (after the resize occurs). So I know that the frames can update their width and height, but the automatic position change is undesirable.

I have been unsuccessful in capturing this change in position through PropertyChanged events, and have scoured through results on Google to try many things (such as overriding the AbsoluteLayout's OnChildMeasureInvalidated() to not let that base method execute). Nothing has worked thus far.

Any ideas? It would be greatly appreciated!

Best Answer

  • bfosterbfoster Member ✭✭
    Accepted Answer

    After more time spent Googling around, I found a method that actually solved my problem.

    I found out that the way that I was updating the position of the child Frames (when the user was moving the Frame), the X and Y of the base class VisualElement was NOT being updated like it should have been. Therefore, the AbsoluteLayout was updating all of the children, and it would magically change its position back to (0,0).

    The following method AbsoluteLayout.SetLayoutBounds(BindableObject bindable, Rectangle bounds) (where bindable is the child Frame object) can be used to update both the position and the width/height when required. :smiley:

Answers

  • bfosterbfoster Member ✭✭
    Accepted Answer

    After more time spent Googling around, I found a method that actually solved my problem.

    I found out that the way that I was updating the position of the child Frames (when the user was moving the Frame), the X and Y of the base class VisualElement was NOT being updated like it should have been. Therefore, the AbsoluteLayout was updating all of the children, and it would magically change its position back to (0,0).

    The following method AbsoluteLayout.SetLayoutBounds(BindableObject bindable, Rectangle bounds) (where bindable is the child Frame object) can be used to update both the position and the width/height when required. :smiley:

Sign In or Register to comment.