Darren Oster
Darren Oster

Reputation: 9196

Custom UIView not working in Xamarin

I am trying to subclass UIView using Xamarin but don't seem to be getting the results I expect. To keep things simple, I have the following class:

using System;
using CoreGraphics;
using Foundation;
using UIKit;

namespace ExerciseTracker.iOS.Views
{
    [Register("CustomView")]
    public class CustomView : UIView
    {
        public override void Draw(CGRect rect)
        {
            base.Draw(rect);
        }
    }
}

A breakpoint is set on the base.Draw(rect); line. A view exists in my Storyboard layout with its class set to CustomView.

enter image description here

However, when the app is run and the page is shown, the breakpoint is never hit, and the breakpoint it displayed as though it will never be hit.

enter image description here

This is using Visual Studio for Mac v7.1.5. Is there anything I'm missing here? It seems like it should be quite simple, but I've been stuck for a few hours on this.

Upvotes: 1

Views: 715

Answers (1)

Kevin Li
Kevin Li

Reputation: 2258

You need to add a construct with IntPtr parameter in CustomView class, like this:

public CustomView(IntPtr p): base(p)
{
    //Your initialization code.
}

Then this class can be used in storyboard directly.

More details you can refer to this Xamarin official documentation: Walkthrough - Using Custom Controls with the Xamarin Designer for iOS.

Upvotes: 2

Related Questions