2012-01-29 52 views
8

我想这段代码转换为C#,代码是从苹果公司的documentation转换从obj获得C到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]; 

到目前为止,这是我的尝试,我被卡在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); 
     } 

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

+0

我不确定你的意思是“我卡在CGRectValue”。调试器是否显示所有先前提到的变量都具有您期望的值?调试器在aRect = self.view.frame之后显示有关aRect的内容? – 2012-01-29 01:50:43

+0

我无法将该行转换为C#代码。它不会编译。请参阅我尝试的C#代码。 – 2012-01-29 02:47:41

+0

如何获得'_currentField'?我也在转换教程* [iOS SDK:从键盘下面保存内容](http://code.tutsplus.com/tutorials/ios-sdk-keeping-content-from-underneath-the-keyboard--mobile -6103)* ... – testing 2014-11-27 11:38:25

回答

5

您的C#代码还有其他错误。

aRect.Size.Height -= kbSize.Height; 

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

你应该做的是:

aRect.Height -= kbSize.Height; 

,这将是真正降低aRect大小(而非Size结构,不会被分配回RectangleF)。

+0

感谢您的好评。这实际上是我翻译中的一个错误。现在,它运行得更好了,但不知道为什么当我开始键入时UITextField仍然有点跳跃,即使它已经在键盘上显示出来了。任何建议? – 2012-01-30 00:11:39

+0

不是没有看到它跳得多么好;-)但是如果你可以创建一个小的,自包含的测试用例来显示行为,然后将它附加到http://bugzilla.xamarin.com上的错误报告,我们将有看看它。 – poupou 2012-01-30 00:17:28

+0

好的,会做的。同时,您是否可以让我知道是否有任何其他此问题的解决方案样例,即在键盘结束时使文本字段可见。我目前找不到一个好的工作示例来进行单点触控。谢谢您的帮助。 – 2012-01-30 00:21:23

13

想通了:

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

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

+1

'var kbSize =((NSValue)info [UIKeyboard.FrameBeginUserInfoKey])。CGRectValue.Size;' – Arvis 2015-03-23 10:09:18