Im using NSTabViewController
to create a preferences window. In order to execute some code when the active tab changes, I would like to use NSTabView
's DidChange
event (as also suggested in Xamarin Forums discussion/comment/118342/#Comment_118342 - unfortunately I can't post links yet).
This is what I have:
var tabViewController = new NSTabViewController(); tabViewController.TabView.DidSelect += (sender, e) => // exception thrown here { // do stuff here };
Unfortunately, an InvalidOperationException is thrown when attaching the event handler:
Event registration is overwriting existing delegate. Either just use events or your own delegate: AppKit.NSTabViewController AppKit.NSTabView+_NSTabViewDelegate
I am actually only using events and not using my own delegate (contrary what the exception message is saying). Or do I misinterpret this?
Anyway, I checked the (generated) source code for NSTabView
(/Library/Frameworks/Xamarin.Mac.framework/Versions/4.4.1.193/src/Xamarin.Mac/AppKit/NSTabView.g.cs). This is what I understand (and I might be wrong...): In order to use the DidSelect
event, the Delegate
property should be of (internal) type _NSTabViewDelegate
. I also saw that if I set Delegate
to null before using DidSelect
, an instance of _NSTabViewDelegate
is created and everything should be okay, so I came up with this:
var tabViewController = new NSTabViewController(); tabViewController.TabView = null; // silently crashing here tabViewController.TabView.DidSelect += (sender, e) => { // do stuff here };
Unfortunately, the app silently crashes without any exception when assigning null.
Can someone explain how DidSelect
event should/can actually be used?
Thanks in advance!
Answers
you can create your own delegate something like that
and you need to initialize TabView
Hi @YuriKuznetsov , thanks for sharing your the idea.
Unfortunately, the app is crashing when I assign the delegate:
tabViewController.TabView.Delegate = dlg;
The unhandled exception is an
NSInternalInconsistencyException
:@johannes_schmitt
Did you try just using a NSTabView instead of the NSTabViewController?
If you want to use the NSTabViewController, I would recommend subclassing it and then overriding the DidSelect method.
A simple sample is attached.