我正在建造一個使用C夏普(與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);
}
}
UPDATE:
它工作時,我讓一個靜態方法,但我需要一個實例傳遞給它的是工作。任何想法爲什麼靜態方法會起作用?
聖地獄Xamarin看起來非常甜。我不知道它存在。你喜歡它嗎? – Jonesopolis
是的,它非常完整,基本上可以滿足您的一切需求。在使用Java for Android一段時間之後,它有點過渡了,有時會出現一些小錯誤,但值得平衡使用C#並能夠在iOS中重用大量代碼。 – clifgray