0

I need to override a function in Xamarin because I get the error:

Illegal Argument Exception - DrawerLayout must be measured with MeasureSpec.EXACTLY

I have derived a class as shown:

using Android.Content;
using Android.Support.V4.Widget;
using Android.Views;

namespace Tut48_4.Droid
{
    public class MyDrawerLayout : DrawerLayout
    {
        public MyDrawerLayout(Context inContext):base(inContext)
        {
        }
        public MyDrawerLayout(
            Context inContext, 
            Android.Util.IAttributeSet attrs):base(inContext,attrs)
        {
        }

        public MyDrawerLayout(
            Context inContext, 
            Android.Util.IAttributeSet attrs, 
            int defStyle) : base(inContext, attrs,defStyle)
        {
        }

        protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            widthMeasureSpec = MeasureSpec.MakeMeasureSpec(
                MeasureSpec.GetSize(widthMeasureSpec), 
                MeasureSpecMode.Exactly);

            heightMeasureSpec = MeasureSpec.MakeMeasureSpec(
                MeasureSpec.GetSize(heightMeasureSpec), 
                MeasureSpecMode.Exactly);

            base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
}

However I get a cast exception using the code:

var mytest = FindViewById(Resource.Id.drawer_layout);
mDrawerLayout = (MyDrawerLayout)FindViewById(Resource.Id.drawer_layout);

First statement works, second fails. I'm new to this so please feel free to ask the obvious questions.

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
bibble235
  • 788
  • 9
  • 15
  • Are you using Xaml? if you are, you should post your xaml. In the meantime, did you read [this question?](https://stackoverflow.com/questions/31746072/drawerlayout-must-be-measured-with-measurespec-exactly-error) (as it's the same error message, looks related) – Tewr Sep 11 '17 at 11:45
  • Shouldn't you be using FindViewById instead of (MyDrawerLayout)FindViewById ? – Kinxil Sep 11 '17 at 12:40
  • I am using Xaml, this does seem a popular question. I have tried the alternative casting but with no joy. – bibble235 Sep 12 '17 at 04:12

0 Answers0