从Obj C转换为C#

AD.*_*Net 8 .net c# objective-c xamarin.ios uikeyboard

我试图将这段代码转换为C#,代码来自Apple的文档

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];
Run Code Online (Sandbox Code Playgroud)

到目前为止,这是我的尝试,我陷入了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);
        }
Run Code Online (Sandbox Code Playgroud)

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

AD.*_*Net 13

弄清楚了:

((NSValue)info[UIKeyboard.FrameBeginUserInfoKey]).RectangleFValue.Size
Run Code Online (Sandbox Code Playgroud)

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


pou*_*pou 5

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

aRect.Size.Height -= kbSize.Height;
Run Code Online (Sandbox Code Playgroud)

SizeSystem.Drawing.SizeF一个结构类型(即一个值类型).更改它的值不会传播回to aRect实例(这是一种.NET行为).

你应该做的是:

aRect.Height -= kbSize.Height;
Run Code Online (Sandbox Code Playgroud)

这将实际减少aRect大小(不是Size不会被分配回的结构RectangleF).