C# Xamarin C如何向AppDelegate添加接口或类?

C# Xamarin C如何向AppDelegate添加接口或类?,c#,ios,xamarin,xamarin.ios,C#,Ios,Xamarin,Xamarin.ios,此处提供了作为项目的示例代码: 如何在AppDelegate中添加EmptyInterface或EmptyClass?应用公共类AppDelegate:UIApplicationDelegate,EmptyInterface或公共类AppDelegate:UIApplicationDelegate,EmptyClass时失败 谁能告诉我正确的路吗 using Foundation; using UIKit; namespace testing { [Register("AppD

此处提供了作为项目的示例代码:

如何在AppDelegate中添加EmptyInterface或EmptyClass?应用公共类AppDelegate:UIApplicationDelegate,EmptyInterface或公共类AppDelegate:UIApplicationDelegate,EmptyClass时失败

谁能告诉我正确的路吗

using Foundation;
using UIKit;

namespace testing {

  [Register("AppDelegate")]

  public class AppDelegate : UIApplicationDelegate {

    public override UIWindow Window {
      get;
      set;
    }
  }
}
编辑:错误代码:

AppDelegate.cs53,53:错误CS0535:“AppDelegate”未实现接口成员“EmptyInterface.EmptyInterfaceMethod1”CS0535测试

AppDelegate.cs53,53:错误CS1721:类“AppDelegate”不能有多个基类:“UIApplicationDelegate”和“EmptyClass”CS1721测试


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

接口: 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。在您的情况下,正确的名称可能会被忽略

班 类AppDelegate不能有多个基类:UIApplicationDelegate和EmptyClass


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

失败是什么意思?它给出了什么错误?类肯定是无效的,此时您只能从一个UIApplicationLegate类继承。但是您应该仍然能够实现一个接口。失败的原因是什么?@YumYumYum自己检查错误,并尝试找出它们的含义。这很直截了当。命名空间“testing”已包含“EmptyInterface”的定义,这意味着您在同一命名空间中定义了2次接口。现在,在应用上次编辑后,它可以正常工作。谢谢在此提交更新: