Xamarin C# 如何将接口或类添加到 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;
}
}
}

编辑:错误代码:

应用程序委托.cs (53,53): 错误 CS0535:"应用程序委托"未实现接口成员"空接口.空接口方法1()"(CS0535)(测试)

应用程序委托.cs(53,53): 错误 CS1721: 类"应用程序委托"不能有多个基类:"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

这个非常简单,你不能有一个从 2 个基类继承的类 (AppDelegate)。这是 C# 的语言语法不支持的。因为这样就会有同名属性或方法等问题。

最新更新