Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion from Obj C to C#

I am trying to convert this piece of code to C#, the code is from Apple's 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];

So far this is my attempt, I am getting stuck at the 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);
        }

I am probably not using the right type, can someone please help me out, or some alternative code doing similar thing. Thanks

like image 524
AD.Net Avatar asked Jan 29 '12 01:01

AD.Net


2 Answers

Figured it out:

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

That should work. Although I could not make it work like I wanted, that line of code would actually compile and translate to the Obj C code.

like image 190
AD.Net Avatar answered Nov 20 '22 13:11

AD.Net


There's something else wrong with your C# code.

aRect.Size.Height -= kbSize.Height;

Size is of type System.Drawing.SizeF which is a struct (i.e. a value-type). Changing it's value won't be propagated back to the to aRect instance (this is a .NET behavior).

What you should do is:

aRect.Height -= kbSize.Height;

which will be reduce actually the aRect size (not the Size struct which won't be assigned back to the RectangleF).

like image 40
poupou Avatar answered Nov 20 '22 11:11

poupou