此处提供项目示例代码:Github Project file
如何在 AppDelegate 中添加我的 EmptyInterface 或 EmptyClass?当我应用 public class AppDelegate : UIApplicationDelegate, EmptyInterface 或 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): Error CS0535: 'AppDelegate' does not implement interface member 'EmptyInterface.EmptyInterfaceMethod1()' (CS0535) (testing)
or
AppDelegate.cs(53,53): Error CS1721: Class 'AppDelegate' cannot have multiple base classes: 'UIApplicationDelegate' and 'EmptyClass' (CS1721) (testing)
最佳答案
错误代码有效,因为您没有以正确的方式完成代码:
- 接口(interface):
AppDelegate
does not implement interface memberEmptyInterface.EmptyInterfaceMethod1()
这告诉您存在名为 EmptyInterface
的现有接口(interface),它具有方法 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 命名接口(interface)。在您的情况下,正确的名称是 IEmptyInterface
。
- 类(class)
Class
AppDelegate
cannot have multiple base classes:UIApplicationDelegate
andEmptyClass
这个非常简单,您不能拥有一个继承自 2 个基类的类 (AppDelegate
)。这是 C# 语言语法不支持的。因为这样就会出现同名属性或方法等问题。
关于c# - Xamarin C# 如何向 AppDelegate 添加接口(interface)或类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48095649/