Bug 14225 - KVC Compliant Class / Observer not notified
Summary: KVC Compliant Class / Observer not notified
Status: RESOLVED ANSWERED
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 6.4.0
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-08-24 08:08 UTC by StephaneC
Modified: 2017-05-19 16:56 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
AppDelegate.cs (2.78 KB, text/plain)
2017-05-19 16:56 UTC, Rolf Bjarne Kvinge [MSFT]
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and 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 Links.

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.

Related Links:
Status:
RESOLVED ANSWERED

Description StephaneC 2013-08-24 08:08:09 UTC
I create a KVC compliant class.

    public class KVCCompliant : NSObject
    {
        public KVCCompliant()
        {
        }

        [Export]
        public string HelloWorld
        {
            get;
            set;
        }
    }

I instantiate an object and add an observer to it.

    KVCCompliant compliant = new KVCCompliant();
    compliant.AddObserver(this, (NSString)"HelloWorld", NSKeyValueObservingOptions.OldNew, IntPtr.Zero);

Then I set the HelloWorld property.

    compliant.SetValueForKey((NSString)"Hello Steve", (NSString)"HelloWorld");
    compliant.HelloWorld = "Hello Steve Ballmer";

The observer is notified when calling SetValueForKey but not when using property setter.

In Objective-C, the observer is notified in both cases.

Same problem with Xamarin.Mac.
Comment 1 Rolf Bjarne Kvinge [MSFT] 2017-05-19 16:56:39 UTC
You must manually notify property changes:

public class KVCCompliant : NSObject
{
	string hello_world;

	[Export ("HelloWorld")]
	public string HelloWorld {
		get {
			return hello_world;
		}
		set {
			base.WillChangeValue ("HelloWorld");
			hello_world = value;
			base.DidChangeValue ("HelloWorld");
		}
	}
}

this works fine for me. I've attached a complete sample file.
Comment 2 Rolf Bjarne Kvinge [MSFT] 2017-05-19 16:56:57 UTC
Created attachment 22315 [details]
AppDelegate.cs