从Obj C转换为C#

时间:2012-01-29 01:33:22

标签: c# .net objective-c xamarin.ios uikeyboard

我正在尝试将这段代码转换为C#,代码来自Apple的documentation

NSDictionary* info = [aNotification userInfo];

CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);

scrollView.contentInset = contentInsets;

scrollView.scrollIndicatorInsets = contentInsets;

CGRect aRect = self.view.frame;

aRect.size.height -= kbSize.height;

if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {

    CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);

    [scrollView setContentOffset:scrollPoint animated:YES];

到目前为止,这是我的尝试,我陷入了CGRectValue。

                    NSDictionary info = n.UserInfo;

        SizeF kbSize = ((RectangleF)info[UIKeyboard.FrameBeginUserInfoKey]).Size;

        UIEdgeInsets contentInsets = new UIEdgeInsets(0.0f, 0.0f, kbSize.Height, 0.0f);

        this.uiScrollView.ContentInset = contentInsets;
        this.uiScrollView.ScrollIndicatorInsets = contentInsets;

        RectangleF aRect = this.View.Frame;

        aRect.Size.Height -= kbSize.Height;

        if(!aRect.Contains(_currentField.Frame))
        {
            PointF scrollPoint = new PointF(0.0f, _currentField.Frame.Y - kbSize.Height);
            this.uiScrollView.SetContentOffset(scrollPoint, true);
        }

我可能没有使用正确的类型,有人可以帮助我,或者一些替代代码做类似的事情。 感谢

2 个答案:

答案 0 :(得分:13)

想出来:

((NSValue)info[UIKeyboard.FrameBeginUserInfoKey]).RectangleFValue.Size

那应该有用。虽然我不能像我想的那样使它工作,但这行代码实际上会编译并转换为Obj C代码。

答案 1 :(得分:5)

您的C#代码还有其他问题。

aRect.Size.Height -= kbSize.Height;

Size属于System.Drawing.SizeF类型,它是一个结构(即值类型)。更改它的值不会传播回到aRect实例(这是一种.NET行为)。

你应该做的是:

aRect.Height -= kbSize.Height;

实际上会减少aRect大小(不是Size结构,不会被分配回RectangleF)。