c# - Xamarin C# 如何向 AppDelegate 添加接口(interface)或类?

标签 c# ios xamarin xamarin.ios

此处提供项目示例代码: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)

最佳答案

错误代码有效,因为您没有以正确的方式完成代码:

  1. 接口(interface):

    AppDelegate does not implement interface member EmptyInterface.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


  1. 类(class)

Class AppDelegate cannot have multiple base classes: UIApplicationDelegate and EmptyClass

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

关于c# - Xamarin C# 如何向 AppDelegate 添加接口(interface)或类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48095649/

相关文章:

Android - 在 ListView 中滚动时滚出/折叠元素

c# - 在 C# 中可以通过字符串访问类吗?

c# - 以文化敏感的方式获取双变量中的小数

c# - Web API2 Identity2 不记名 token 权限更改

ios - Int64存储在Realm中无法转换

ios - 在他拒绝后请求知道他们的位置

c# - 在 TLS 加密下使用 DefaultNetworkCredential?

ios - 共享 iAd 横幅

iOS推送通知自定义声音无法弄清楚

android - 如何在 Android 上修复 "Only the original thread that created a view hierarchy can touch its views"Xamarin Forms