簡體   English   中英

Xamarin C#如何為AppDelegate添加接口或類?

[英]Xamarin C# how do i add interface or class to AppDelegate?

可在此處找到作為項目的示例代碼: Github項目文件

如何在AppDelegate中添加EmptyInterface或EmptyClass? 當我應用public class AppDelegate : UIApplicationDelegate, EmptyInterface or public class AppDelegate : UIApplicationDelegate, EmptyClass它失敗

誰能告訴我正確的方法嗎?

using Foundation;
using UIKit;

namespace testing {

  [Register("AppDelegate")]

  public class AppDelegate : UIApplicationDelegate {

    public override UIWindow Window {
      get;
      set;
    }
  }
}

編輯:錯誤代碼:

AppDelegate.cs(53,53):錯誤CS0535:“ AppDelegate”未實現接口成員“ EmptyInterface.EmptyInterfaceMethod1()”(CS0535)(正在測試)

要么

AppDelegate.cs(53,53):錯誤CS1721:類'AppDelegate'不能具有多個基類:'UIApplicationDelegate'和'EmptyClass'(CS1721)(測試)

錯誤代碼是有效的,因為您尚未以正確的方式完成該代碼:

  1. 介面

    AppDelegate沒有實現接口成員EmptyInterface.EmptyInterfaceMethod1()

這個告訴您,有一個名為EmptyInterface現有接口,該接口具有方法EmptyInterfaceMethod1 ,並且您必須在代碼中實現(使用)該接口。

解:

interface IEmptyInterface {

    int EmptyInterfaceMethod1();

}

public class AppDelegate : UIApplicationDelegate, IEmptyInterface {

    public override UIWindow Window { get; set; }

    //The type and parameter have to be same as in the interface!
    public int EmptyInterfaceMethod1() { return 1; }

}

請注意 ,通用方法是在名稱的開頭使用大寫字母I的名稱接口。 在您的情況下,正確的名稱為IEmptyInterface


AppDelegate類不能具有多個基類: UIApplicationDelegateEmptyClass

這很簡單,您不能有一個類( AppDelegate ),該類繼承自2個基類。 這是C#的語言語法不支持的。 因為那樣會出現同名屬性或方法等問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM