问题
I am using xamarin to develop a Mac app, and somewhere in my program I want to set the tag of a NSView. However, the tag property is readonly for NSView, so I'm searching for a way to create a subclass where tag is writable. Is there any suggestion about how I should write the subclass? thanks
回答1:
public class MyNSView : NSView
{
public nint _tag;
public new nint Tag
{
get
{
return _tag;
}
set
{
_tag = value;
}
}
Be aware that this is not longer the NSView
Tag.
Using your custom NSView
Tag
var view = new MyNSView ();
view.Tag = 100;
回答2:
I found a workaround to get viewWithTag
working:
@IBInspectable
override var tag: Int {
get { return super.tag }
set { super.tag = newValue }
}
In IB the NSView's Tag is still greyed out, but there is also a subclass's Tag available for setting a value.
Still, to my best effort, I can't find a logical reason for Tag to be greyed out in IB.
来源:https://stackoverflow.com/questions/44377881/create-a-subclass-of-nsview-to-enable-settag