Skip to content

Instantly share code, notes, and snippets.

@rzaitov
Last active August 29, 2015 14:03
Show Gist options
  • Save rzaitov/bfb3c273e4e6e2c8bc64 to your computer and use it in GitHub Desktop.
Save rzaitov/bfb3c273e4e6e2c8bc64 to your computer and use it in GitHub Desktop.
Dependent key
public partial class MyController : UIViewController
{
private bool deviceAuthorized {
[Export("isDeviceAuthorized")]
get;
[Export("setDeviceAuthorized:")]
set;
}
private bool sessionRunningAndDeviceAuthorized {
[Export("isSessionRunningAndDeviceAuthorized")]
get {
return deviceAuthorized;
}
}
[Export("keyPathsForValuesAffectingSessionRunningAndDeviceAuthorized")]
private static NSSet keyPathsForValuesAffectingSessionRunningAndDeviceAuthorized ()
{
return NSSet.MakeNSObjectSet (new NSString[] {
(NSString)"deviceAuthorized",
});
}
public override void ViewWillAppear (bool animated)
{
base.ViewWillAppear (animated);
NSKeyValueObservingOptions oldNew = NSKeyValueObservingOptions.Old | NSKeyValueObservingOptions.New;
NSKeyValueObservingOptions initOldNew = NSKeyValueObservingOptions.Initial | NSKeyValueObservingOptions.Old | NSKeyValueObservingOptions.New;
AddObserver (this, (NSString)"sessionRunningAndDeviceAuthorized", oldNew, SessionRunningAndDeviceAuthorizedContext);
// after this call I expect ObserveValue method invokation.
deviceAuthorized = !deviceAuthorized;
}
public override void ObserveValue (NSString keyPath, NSObject ofObject, NSDictionary change, IntPtr context)
{
}
}
@rzaitov
Copy link
Author

rzaitov commented Jun 30, 2014

I found solution, but I think it is not great.
I can surround deviceAuthorized = !deviceAuthorized; with WillChangeValue / DidChangeValue:

WillChangeValue ("deviceAuthorized");
deviceAuthorized = !deviceAuthorized;
DidChangeValue ("deviceAuthorized");

But why [Export()] doesn't do all job?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment