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.

Android Custom Renderer is not changing height after PropertyChangedEvent

AdrnHAdrnH USMember ✭✭

Hi,

there are 2 views on a page. The views are inside a StackLayout. The views are Grids. One Row of the Grid is MyView which is renderer by a custom renderer.
By clicking onone view the properties of the MyView is swapped. MyView one is displaying the content of MyView two and the other way around.
However, after changing the property the height of MyView is not adjusted correctly to it's content depending on their property. My View is keeping the height of MyView from it's previous content.

I think the problem is inside the custom renderer which is not changing the height of MyView correctly after a PropertyChangedEvent.

[assembly: ExportRenderer(typeof(MyView), typeof(MyViewDroidRenderer))]
namespace pnyxnetmobile.Droid.Renderer
{
    public class DroidRenderer : ViewRenderer
    {
        private MyLinearLayoutBuilderAndroid _builder;

        public DroidRenderer(Context context) : base(context)
        {
            //Init Builder
            _builder = new MyLinearLayoutBuilderAndroid(context);
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.View> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
                MyView view = e.NewElement as MyView;
                //Generate a LinearLayout
                SetNativeControl(_builder.GenerateLinearaLayout(view));
            }
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
            MyView view = sender as MyView;
            //Value is the property which is changed
            if (MyView.ValueProperty.PropertyName.Equals(e.PropertyName))
            {
                if (view != null){
                    SetNativeControl(_builder.GenerateLinearaLayout(view));
                    Control.RequestLayout();
                }
            }
        }
    }
}

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    The views are inside a StackLayout. The views are Grids. One Row of the Grid is MyView which is renderer by a custom renderer.

    According to my understand, the control hierarchy is: StackLayout->Grids ->MyView ,and the MyView is renderer by a custom renderer,right?

    By clicking onone view the properties of the MyView is swapped. MyView one is displaying the content of MyView two and the other way around.

    But I am not understand above words, could you elaborate a bit more about on this?
    If it is convenient for you ,could you please post some screenshots or video to show your question?

  • AdrnHAdrnH USMember ✭✭

    @jezh said:

    The views are inside a StackLayout. The views are Grids. One Row of the Grid is MyView which is renderer by a custom renderer.

    According to my understand, the control hierarchy is: StackLayout->Grids ->MyView ,and the MyView is renderer by a custom renderer,right?

    yes

    @jezh said:

    By clicking onone view the properties of the MyView is swapped. MyView one is displaying the content of MyView two and the other way around.

    But I am not understand above words, could you elaborate a bit more about on this?
    If it is convenient for you ,could you please post some screenshots or video to show your question?

    As I am not comfortable posting work projects i tried to make a sketch to illustrate the issue.

    By clicking somewhere in Grid1 the content of the Grids swap. The content of State1.Grid1 is now in State2.Grid2 and the the content of of State1.Grid2 is now in State1.Grid1. However the height of MyView1 is not changing. The height of some other views would change, but they work with xamarin controls not with custom renderer.

  • jezhjezh Member, Xamarin Team Xamurai

    Why not set the Height to Auto ?

    When we check official document Rows and columns,we will find:

    Auto – the row height or column width is autosized based on the cell contents (Auto in XAML).

Sign In or Register to comment.