سؤال

i am new to Xamarin monoandroid and i am facing a problem in adding a custom view to my layout. i have no idea what i am doing wrong. little help will be great full and this is my custom view class

onDraw method is called but i don't get the output and no errors.

class PositionDrawer : View
    {
        private int count;
        private int position;
        private IPositionDrawer cinterface;
        private Activity cactivity;
        private int container;
        private int p;
        private ScoreFeed scoreFeed1;
        private ScoreFeed scoreFeed2;
        public PositionDrawer(Activity context, int count, int positionr, Activity cactivity) :
            base (context)
        {
            //Initialize ();
            this.cactivity = cactivity;
            this.cinterface = cinterface;
            this.count = count;
            this.position = positionr;
            //this.OnDraw();
            Initialize();
        }

        private void Initialize()
        {
            //this.Invalidate();
           // this.SetMeasuredDimension(100, 100);
            this.SetWillNotDraw(false);
            Console.WriteLine(this.WillNotDraw());
        }
        protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
        }
        public void SetDrawer(int count, int positionr, Activity cactivity)

        {

           this.cactivity = cactivity;
           this.cinterface = cinterface;
           this.count = count;
           this.position = positionr;

           //base(cactivity);   
        }

         protected override void OnDraw(Canvas canvas)
        {
            base.OnDraw(canvas);
            Paint blue = new Paint();
            blue.Color = Color.Red;

            float screenwidth = cactivity.WindowManager.DefaultDisplay.Width;
            canvas.DrawRect(screenwidth * position-1, 0, screenwidth * position, 20, blue);
            Console.WriteLine("SW1:" + screenwidth + "pos1:" + position);
           // LinearLayout can =cactivity.FindViewById<LinearLayout>(container);
            //cinterface.getview(this);
        }

    }

and i use following code to add view to the layout.

 View pd = new PositionDrawer(this, 3, position, this);
 container = FindViewById<LinearLayout>(Resource.Id.position);
 container.RemoveAllViews();
 container.AddView(pd);

thanks in advance.

هل كانت مفيدة؟

المحلول

You need to implement the correct ctors.

When you implement the View class then you have to at least have the ctor with the following signature:

public PositionDrawer(IntPtr javaReference, JniHandleOwnership transfer)
    : base(javaReference, transfer) { }

And if you are using the View in a AXML layout you also need the one with the following signature:

public PositionDrawer(Context context, IAttributeSet attrs, int defStyle)
    : base(context, attrs, defStyle)

Otherwise when you create Views from code, you need to give them some LayoutParameters otherwise they do not know their own size.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top