Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
Mono organizations on
GitHub to continue tracking issues. Bugzilla will remain
available for reference in read-only mode. We will continue to work
on open Bugzilla bugs, copy them to the new locations
as needed for follow-up, and add the new items under Related
Our sincere thanks to everyone who has contributed on this bug
tracker over the years. Thanks also for your understanding as we
make these adjustments and improvements for the future.
Please create a new report on
Developer Community or GitHub with
your current version information, steps to reproduce, and relevant error
messages or log files if you are hitting an issue that looks similar to
this resolved bug and you do not yet see a matching new report.
Created attachment 15284 [details]
I am adding an observer either like:
var observable = view.AddObserver(new NSString("hidden"), NSKeyValueObservingOptions.OldNew, OnHidden);
view.AddObserver(this, new NSString("hidden"), NSKeyValueObservingOptions.OldNew, IntPtr.Zero);
in the latter case I have overridden the ObserveValue(NSString, NSObject, NSDictionary, IntPtr) method.
Either observer does not fire when changing the Hidden property of the view is changed.
If I do the same in a Xcode Playground using Swift, the observer does get triggered. Why is there a difference between Swift and Xamarin?
I've also tried listening to isHidden and setHidden to no avail. It seems like the UIView implementation is missing WillChangeValue and DidChangeValue in its properties.
Hmm, after playing around with the same code in a new project I cannot reproduce. Still investigating.
Created attachment 15286 [details]
I've attached a Xamarin.iOS project reproducing the problem.
It seems like having multiple degrees of nesting triggers this issue.
I.e. when having a hierarchy like this:
When adding an observer in View1 for View2 and View3 for "hidden" ObserveValue won't get triggered. This is only if View2 and View3 are custom views like so:
public class View2 : UIView
public View2(CGRect frame) : base(frame)
Explicitly overriding View2's Hidden property and calling WillChangeValue and DidChangeValue on hidden triggers the ObserveValue in View1.
How come it is necessary to explicitly implement KVO for properties you don't touch when inheriting from NSObjects?
Created attachment 15290 [details]
Swift playground 2
I've uploaded a new playground. It shows that in Swift nested Views even though not explicitly implementing KVO for hidden, still triggers the ObserveValue method. This is not the case in the Xamarin.iOS project.
Hello Tomasz! Glad to see you filling interesting bugs ;)
So I gave a try to your example and indeed one step further in the hierarchy does not work KVO'ing (new verb!) a UIKit object but I think it has something to do with the fact that UIKit framework does not support KVO as documentation states here (not super clear but that gives us a hint)
> Note: Although the classes of the UIKit framework generally
> do not support KVO, you can still implement it in the
> custom objects of your application, including custom views.
Fortunately in this case CALayer is KVO compliant as stated in this doc so all you need (and should) do is kvo the layer property.
From your XS Project attachment you need to change the following lines from ViewController.cs:
Change Line 42 to: (just added `Layer` after `_hideShowView`)
> _hideShowView.Layer.AddObserver (this, "hidden",
> NSKeyValueObservingOptions.OldNew, IntPtr.Zero);
Remove Line 46: (you should not call base here or an exception will be raised. System will think you did not manage your subscribed kVO's if you call base)
Once you do those two changes you should be fine and your sample should run as expected. Do not forget to remove your observers once you do not need them anymore in order to avoid random crashes.
Also do not forget you can use a much more delightful C# friendly Api
Hope this helps!
Using the Layer did indeed work.
For future reference, you can get hold of the UIView by looking at the WeakDelegate property of the Layer.