如何转换此代码以检测相机焦点?

时间:2015-08-26 13:40:04

标签: ios xamarin key-value-observing

我试图将用于检测相机焦点的代码从Xcode / Swift转换为Xamarin / C#。 [阿伦的办公室政治很棒吗?]它似乎没有工作,我不知道为什么。

Swift Code:

private var _doneFocusing = false
let kFocusKeyPath = "adjustingFocus"

// this line in my startCamera method to watch for focus:
captureDevice.addObserver(self, forKeyPath:kFocusKeyPath, options: NSKeyValueObservingOptions.New, context: nil)


override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
    if keyPath == kFocusKeyPath {
        if let changeValue = change[NSKeyValueChangeNewKey] as? NSNumber {
            var adjustingFocus = (changeValue == 1)
            if adjustingFocus {
                _doneFocusing = false
            } else {
                _doneFocusing = true
            }
        }
    }
}

C#代码。根本没有调用ObserveValue方法。

private bool _doneFocusing = false;
private const string kFocusKeyPath = "FocusKeyPath";

// this line in my startCamera method to watch for focus:
_captureDevice.AddObserver(this, kFocusKeyPath, NSKeyValueObservingOptions.New, IntPtr.Zero);


public override void ObserveValue(NSString keyPath, NSObject ofObject, NSDictionary change, IntPtr context)
{
    System.Diagnostics.Debug.WriteLine(keyPath);

    if (keyPath == kFocusKeyPath)
    {
        var changeValue = change.ValueForKey(ChangeNewKey) as NSNumber;
        if (changeValue == null)
        {
            return;
        }
        var adjustingFocus = (changeValue.Int16Value == 1);
        if (adjustingFocus)
        {
            _doneFocusing = false;
        }
        else
        {
            _doneFocusing = true;
        }

    }
}

为了让这项工作正常,我有什么遗漏吗?

1 个答案:

答案 0 :(得分:2)

我不知道为什么我改变了keyPath常量,但这就是问题所在。

keyPath必须具体为&#34; adjustFocus&#34;