【问题标题】:Does not contain a definition and no extension method could be found不包含定义,也找不到扩展方法
【发布时间】:2013-06-12 19:06:25
【问题描述】:

我正在使用 C Sharp(使用 Xamarin)构建一个 android 应用程序,并且我试图从当前名为 FeedActivity.cs 的另一个类中调用一个方法,但我遇到了一个相当普遍的错误:

"FeedActivity:PostPicture:  Exception of type 'Java.Lang.RuntimeException' was thrown."

这是一个 Java 错误,因为 Xamarin 将 C# 编译为 Java,以便它可以在 Android 上运行。

代码如下:

#class from which I want to call the method from the FacebookActivity.cs class
public class FeedActivity : BaseActivity
    {

 #the method that actually starts the whole process
 private void PostPicture(pic picture, bool shareOnFacebook) 
    {
       #posts the picture to my own app, no problem here

       #try catch statement, this is where the error comes in
       try
           {
              if (shareOnFacebook) SharePictureOnFacebook(picture);
           }
       catch (Exception ex)
           {
              LogManager.Error(this, ex, "FeedActivity:PostPicture:  ", ex.Message);
           }
    }

 #method from which I want to call the method from the FacebookActivity.cs class
 private void SharePictureOnFacebook(pic picture)
        {
            FacebookActivity permissionAdder = new FacebookActivity();

            #this is the line that is giving me the error
            permissionAdder.AddPermissions()

            #some more code that isn't relevant
        }
     }

#class that I want to use the method from
public class FacebookActivity : BaseActivity
    {
       #method I want to call from FeedActivity.cs in the SharePictureOnFacebook method
       public void AddPermissions() 
       {
          Authorize(this, AdditionalPermissions);
       }
    }

更新:

当我将它设为静态方法时它可以工作,但我需要将一个实例传递给它才能工作。知道为什么静态方法会起作用吗?

【问题讨论】:

  • 神圣的 Xamarin 看起来真的很可爱。我不知道它存在。你喜欢吗?
  • 是的,它非常完整,基本上拥有您可能需要的一切。在为 Android 使用 Java 一段时间后,这是一个小小的转变,有时会出现一些小错误,但使用 C# 并能够为 iOS 重用大量代码是值得的。

标签: c# java android xamarin


【解决方案1】:

根据发布的代码,这应该可以工作。但是,如果您在不同的命名空间中有相同的类名,我之前遇到过这种情况……而这就是正在使用的类名。

【讨论】:

  • 嗯,好吧,那里没有冲突。它必须是 Xamarin 的特定内容
  • 它适用于静态方法,但实际上不适用于我所需要的,因为我需要一个实例,但知道为什么会这样吗?
  • 我想不出任何原因,因为您正在使用实例调用它。除非该实例被读取为一种类型?
猜你喜欢
  • 1970-01-01
  • 2015-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-09
  • 2018-09-24
  • 1970-01-01
相关资源
最近更新 更多