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

标签 ios xamarin key-value-observing

我正在尝试将用于检测相机焦点的代码从 Xcode/Swift 转换为 Xamarin/C#。 [办公室政治不是很好吗?]它似乎不起作用,我不确定为什么。

快速代码:

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;
        }

    }
}

我是否遗漏了什么才能使它正常工作?

最佳答案

不确定为什么我更改了 keyPath 常量,但这就是问题所在。

keyPath 必须特别是“adjustingFocus”

关于ios - 如何转换此代码以检测相机焦点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32228283/

相关文章:

ios - 如果包含匹配对象,则要合并的数组的数组

ios - 明确反对 UIViewAnimationOptionOverrideInheritedDuration?

ios - 在 iOS 6 上保存联系人时出现 Phonegap ContactError

ios - 如何从 View Controller 访问 AppDelegate 中声明的导航 Controller ?

pointers - 使用 Swift 为没有指针的 KVO 添加观察者

objective-c - 如何以编程方式监视 KVC 对象?

ios - 在 ios 中弹出 View

android - Xamarin Android Native 在/system/lib/libc.so 崩溃

c# - Wcf 服务和 Xamarin PCL : data security

cocoa - 使用 KVO 绑定(bind)到 bool 属性的负数