2012-01-29 49 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