2012-01-29 28 views
8

estoy tratando de convertir esta pieza de código para C#, el código es de Apple documentationLa conversión de Obj C a C#

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

Hasta ahora, este es mi intento, me queda pegada en el 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); 
     } 

Probablemente no estoy usando el tipo correcto, alguien puede ayudarme, o algún código alternativo haciendo algo similar. Gracias

+0

No estoy seguro de lo que quiere decir con "Me estoy atascado en CGRectValue". ¿El depurador muestra que todas las variables referidas anteriormente tienen los valores que esperarías? ¿Qué muestra el depurador sobre aRect after aRect = self.view.frame? –

+0

No puedo convertir esa línea al código C#. No compila Por favor, mira el código de C# que probé. –

+0

¿Cómo se obtiene '_currentField'? También estoy convirtiendo el tutorial * [iOS SDK: mantener el contenido debajo del teclado] (http://code.tutsplus.com/tutorials/ios-sdk-keeping-content-from-underneath-the-keyboard--mobile -6103) * ... – testing

Respuesta

5

Hay algo más erróneo con su código C#.

aRect.Size.Height -= kbSize.Height; 

Size es de tipo System.Drawing.SizeF que es una estructura (es decir, un tipo de valor). Cambiar su valor no se propagará a la instancia aRect (este es un comportamiento .NET).

Lo que debe hacer es:

aRect.Height -= kbSize.Height; 

que será realmente reducir el tamaño aRect (no el Size estructura que no se asigna de nuevo a la RectangleF).

+0

Gracias por una buena captura. Eso en realidad fue un error en mi traducción. Ahora, está funcionando mejor, pero no estoy seguro de por qué el UITextField todavía salta un poco cuando empiezo a escribir, aunque ya esté visible justo encima del teclado. ¿Cualquier sugerencia? –

+0

No sin ver qué tan bien salta ;-) pero si puedes crear un pequeño caso de prueba que muestre el comportamiento, adjúntalo a un informe de error en http://bugzilla.xamarin.com y tendremos un vistazo. – poupou

+0

De acuerdo, será suficiente. Mientras tanto, ¿podría informarme si existe alguna otra solución para este problema, es decir, hacer visible el campo de texto cuando el teclado está sobre él? No pude encontrar una buena muestra de trabajo hasta ahora para monotouch. Gracias por la ayuda. –

13

lo descubrió:

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

que debería funcionar. Aunque no pude hacer que funcionara como quería, esa línea de código compilaría y traduciría al código Obj C.

+1

'var kbSize = ((NSValue) info [UIKeyboard.FrameBeginUserInfoKey]). CGRectValue.Size;' – Arvis