iOS學習系列 – MonoTouch綁定原生Obj-C靜態庫的實現

來源:互聯網
上載者:User

MonoTouch是利用C#語言進行iOS開發的跨平台解決方案,包括支援iPhone/iPad的開發,目前也支援了最新的iOS 6的版本。

官方地址:http://xamarin.com/

github Sample:https://github.com/xamarin/monotouch-samples

app應用:http://xamarin.com/apps/all

MonoTouch可以利用C# 語言進行iOS開發,意味著作為.Net程式員,也可以很容易地進行iOS開發。當然,能夠理解objective-c文法對你的iOS學習也是很有協助的。

 

本篇文章,主要教你如何利用MonoTouch來綁定原生的Obj-C靜態庫,並且直接調用靜態庫裡面的方法完成實現。

首先利用xcode建立一個原生的靜態庫:

Project中加入MBProgressHUD的兩個檔案(MBProgressHUD是個等待載入特效架構,github:https://github.com/jdg/MBProgressHUD)

然後編譯下,預設地,在/Library/Developer/Xcode/DerivedData可以找到BindingLibraryl的應用,libBindingLibrary.a

接著開啟MonoDevelop,建立一個BindingLibrary解決方案,添加一個BindingLibrarySDK項目:

選擇“MonoTouch Binding Project",這個項目專門用來綁定a native library的。

然後,將libBindingLibrary.a添加到該項目中去,並且,你會發現在項目中它會自動產生一個叫做libBindingLibrary.linkwith.cs的檔案:

using System;
using MonoTouch.ObjCRuntime;
 
[assembly: LinkWith ("libBindingLibrary.a", LinkTarget.Simulator, ForceLoad = true)]

這樣就實現了libBindingLibrary.a靜態庫的匯入,此實作類別似於DllImport引用非C#(例如C++)動態庫的匯入。

你可以發現項目中自動產生兩個檔案,ApiDefinition.cs以及StructsAndEnums.cs檔案,ApiDefintion.cs主要作為原生類定義的相關映射。

具體文檔可以查看:Binding Objective-C Types

這裡編寫代碼:

[BaseType (typeof (UIView))]
interface MBProgressHUD
{
[Static, Export("showHUDAddedTo:animated:")]
MBProgressHUD ShowHUDAddedTo (UIView view, bool animated);
 
[Export("show:")]
void Show (bool animated);
 
[Export("hide:")]
void Hide (bool animated);
 
[Export("hide:afterDelay:")]
void Hide (bool animated, double delay);
 
[Export ("labelText", ArgumentSemantic.Copy)]
String LabelText { get; set; } 

其中對應的原生介面有:

+ (MBProgressHUD *)showHUDAddedTo:(UIView *)view animated:(BOOL)animated;
- (void)show:(BOOL)animated;
- (void)hide:(BOOL)animated;
- (void)hide:(BOOL)animated afterDelay:(NSTimeInterval)delay;
@property (copy) NSString *labelText;

其中BaseType為它的繼承類型為UIView,對應於原生的MBProgressHUD類的基類為UIView;

Export為匯入的方法名稱,Static說明它作為一個靜態方式;

ArgumentSematic是它的參數類型為Copy;

 

然後就可以直接編譯BindingLibrarySDK了,產生一個BindingLibrarySDK.dll

 

現在,建立一個新的iOS項目:

這裡,我選擇一個單視窗的iphone應用程式。

把剛剛的BindingLibrarySDK.dll引用進來:

接著,我在xib檔案設計一個按鈕控制項,產生點擊事件的代碼:

在點擊事件代碼中,實現:

partial void showWindow (MonoTouch.Foundation.NSObject sender)
{
    BindingLibrarySDK.MBProgressHUD hud =     BindingLibrarySDK.MBProgressHUD.ShowHUDAddedTo(this.View, true);
hud.Show(true);
hud.LabelText = ;
hud.Hide(true, 1);  // hide hud after 1s 
}

最後,運行程式,查看效果:

點擊按鈕,顯示載入效果框,等1秒之後,效果框消失。

 

這樣就通過MonoTouch實現了綁定原生Obj-C靜態庫,調用了相關的原生方法。

本文例子:https://github.com/sunleepy/monotouch_binding

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.