2016-08-01 54 views
1

我已經爲作爲SDK的一部分的java庫創建了一個綁定,該掃描器是霍尼韋爾Dolphin CT50的型號。然而,在我的理解中,我遇到了一點問題。接口在C#和Java中實現差異

以下示例在Java中給出。 Create方法需要一個接口實現。但是,我不知道類似的方式來覆蓋接口的'onCreated'方法,同時仍然訪問類的變量。這是我有麻煩,我如何在C#中執行以下操作。

的Java:

private static BarcodeReader barcodeReader; 
private AidcManager manager; 

public void onCreate(Bundle savedInstanceState) { 
    // create the AidcManager providing a Context and a 
    // CreatedCallback implementation. 
    AidcManager.create(this, new CreatedCallback() { 
     @Override 
     public void onCreated(AidcManager aidcManager) { 
      manager = aidcManager; 
      barcodeReader = manager.createBarcodeReader(); 
     } 
    }); 

} 

在.NET中,編譯器將不會如上做,實現這個接口作爲一個匿名類型,因此,我必須實現的接口。但是,我怎麼才能分配類的變量?

我的C#:

NameSpace Scanner { 
    private static BarcodeReader barcodeReader; 
    private AidcManager manager; 
    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 
     // Set our view from the "main" layout resource 
     SetContentView(Resource.Layout.Main); 

     CreateCallback callback = new CreateCallback() { }; 
     AidcManager.Create(this, callback.OnCreated(AidcManager aidcManager)); 
    } 

public class CreateCallback : AidcManager.ICreatedCallback 
{ 
    public IntPtr Handle 
    { 
     get 
     { 
      throw new NotImplementedException(); 
     } 
    } 
    public void Dispose() 
    { 
     throw new NotImplementedException(); 
    } 
    public void OnCreated(AidcManager p0) 
    { 
     // How can I initialize the manager and barcode reader variables in the class above from here? 
    } 
    } 
} 
+0

因此,澄清,你知道如何在Java中做到這一點,並要求如何在C#中做到這一點? – chrylis

+0

好點。我將編輯。我想知道如何在C#中執行此操作 –

+0

C#沒有匿名類。您必須將想要捕獲的參數傳遞給子類的構造函數。我不是100%的C#語法,但我相信語法爲'new CreateCallback(){};''{}'是一個初始化列表。 –

回答

4

您可以創建具有其他回調的構造函數的參數successAction一個回調實現CreateCallback。通過回調OnManagerCreated將在CreateCallbackOnCreated中調用。

重要說明:如果您實現Java接口,則必須繼承Java.Lang.Object。做不是實施HandleDispose()在你自己的。

活動

public class MyActivity : Activity 
{ 
    private static BarcodeReader _barcodeReader; 
    private AidcManager _manager; 
    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 
     // Set our view from the "main" layout resource 
     SetContentView(Resource.Layout.Main); 

     CreateCallback callback = new CreateCallback(OnManagerCreated) { }; 
     AidcManager.Create(this, callback); 
    } 

    private void OnManagerCreated(AidcManager manager) 
    { 
     _manager = manager; 
     _barcodeReader = manager.CreateBarcodeReader(); 
    } 
} 

回調實現

public class CreateCallback : Java.Lang.Object, AidcManager.ICreatedCallback 
{ 
    private Action<AidcManager> _successAction; 
    public CreateCallback(Action<AidcManager> successAction) 
    { 
     _successAction = successAction; 
    } 

    public void OnCreated(AidcManager p0) 
    { 
     _successAction(p0); 
    } 
} 

其他可能性

您也可以直接實現接口在活動類像

public class MyActivity : Activity, AidcManager.ICreatedCallback 
{ 
    // ... 
} 
+0

謝謝!這非常有幫助 –

0

你必須對外部類的實例傳遞給構造函數:

class test 
{ 
    private static BarcodeReader barcodeReader; 
    private AidcManager manager; 

    public virtual void onCreate(Bundle savedInstanceState) 
    { 
     // create the AidcManager providing a Context and a 
     // CreatedCallback implementation. 
     AidcManager.create(this, new CreatedCallbackAnonymousInnerClass(this)); 
    } 

    private class CreatedCallbackAnonymousInnerClass : CreatedCallback 
    { 
     private readonly test outerInstance; 

     public CreatedCallbackAnonymousInnerClass(test outerInstance) 
     { 
      this.outerInstance = outerInstance; 
     } 

     public override void onCreated(AidcManager aidcManager) 
     { 
      outerInstance.manager = aidcManager; 
      barcodeReader = outerInstance.manager.createBarcodeReader(); 
     } 
    } 
}