C# 方法之间的模糊调用
我尝试在活动之间传递数据,但出现错误: C:\Users\nemesis\Documents\GitHub\Murakami_kiev\MurakamiKiev\Menu Activities\SoupesActivity.cs(5,5):错误CS0121:以下方法或属性之间的调用不明确:“Android.Content.Intent.PutExtra(string,short)”和“Android.Content.Intent.PutExtra(string,bool)”(CS0121)(MurakamiKiev) 活动1中的代码:C# 方法之间的模糊调用,c#,android,xamarin,C#,Android,Xamarin,我尝试在活动之间传递数据,但出现错误: C:\Users\nemesis\Documents\GitHub\Murakami_kiev\MurakamiKiev\Menu Activities\SoupesActivity.cs(5,5):错误CS0121:以下方法或属性之间的调用不明确:“Android.Content.Intent.PutExtra(string,short)”和“Android.Content.Intent.PutExtra(string,bool)”(CS0121)(Mu
private void ParseAndDisplay1(JsonValue json) {
TextView productname = FindViewById<TextView> (Resource.Id.posttittle);
TextView price = FindViewById<TextView> (Resource.Id.price);
TextView weight = FindViewById<TextView> (Resource.Id.weight);
productname.Click += delegate {
var intent404 = new Intent (this, typeof(SoupesDetailActivity1));
JsonValue firstitem = json [81];
intent404.PutExtra ("title", firstitem ["post_title"]);
intent404.PutExtra ("price", firstitem ["price"] + " грн");
intent404.PutExtra ("weight", firstitem ["weight"] + "г");
StartActivity (intent404);
};
}
private void ParseAndDisplay1(JsonValue-json){
TextView productname=FindViewById(Resource.Id.PostTitle);
TextView price=findviewbyd(Resource.Id.price);
TextView-weight=findviewbyd(Resource.Id.weight);
productname。单击+=委派{
var intent404=新的意图(此,类型为(SoupesDetailActivity1));
JsonValue firstitem=json[81];
intent404.PutExtra(“标题”,第一项[“后标题]);
意向404.PutExtra(“价格”,第一项[“价格”]+“Γаа”);
意图404.PutExtra(“重量”,第一项[“重量”]+“Γ”);
起始触觉(intent404);
};
}
接收属性时Activity2中的代码:
private void ParseAndDisplay(JsonValue json) {
TextView productname = FindViewById<TextView>(Resource.Id.posttitle);
TextView content = FindViewById<TextView>(Resource.Id.postcontent);
TextView price = FindViewById<TextView>(Resource.Id.price);
TextView weight = FindViewById<TextView>(Resource.Id.weight);
//JsonValue firstitem = json[81];
productname.Text = Intent.GetStringExtra("title");
content.Text = firstitem["post_excerpt"];
price.Text = firstitem["price"] + " грн";
weight.Text = firstitem["weight"] + "г";
}
private void ParseAndDisplay(JsonValue-json){
TextView productname=FindViewById(Resource.Id.posttitle);
TextView content=findviewbyd(Resource.Id.postcontent);
TextView price=findviewbyd(Resource.Id.price);
TextView-weight=findviewbyd(Resource.Id.weight);
//JsonValue firstitem=json[81];
productname.Text=Intent.GetStringExtra(“标题”);
content.Text=firstitem[“post_摘录”];
price.Text=firstitem[“price”]+“Гцц”;
weight.Text=第一项[“weight”]+“Γ”;
}
代码有什么问题
感谢您的帮助发生此错误是因为编译器没有指定足够的数据来理解调用哪个方法。
要解决任务,必须在PutExtra方法调用中显式指定变量类型:
intent404.PutExtra ("title", (firstitem ["post_title"]).ToString());
intent404.PutExtra ("price", (firstitem ["price"] + " грн").ToString());
intent404.PutExtra ("weight", (firstitem ["weight"] + "г").ToString());
JsonValue
继承自object
,当您调用intent404.PutExtra
时,.NET不知道他将调用哪个方法,要解决您的问题,您只需强制转换对象,如下所示:
intent404.PutExtra("title", (string)firstitem["post_title"]);
或
MSDN上的Json值:
我认为它会解决您的问题。前面的答案说明了如何解决问题,但错误消息令人困惑,因此我将描述这是如何发生的 考虑一下这种方法
private static void Bar(short value) { }
private static void Bar(bool value) { }
编译器应该决定运行什么方法,并根据参数类型决定
编译器将选择第一种方法:
short s = 0;
Bar(s);
编译器将选择第二种方法:
bool b = true;
Bar(b);
若编译器找不到适合您的参数类型的方法,它将给您一个错误:
object obj = new object();
Bar(obj);
无法从“object”转换为“short”
然后尝试将firstitem[“post_title”]
作为方法参数传递。此参数的类型为。您得到不明确调用
错误的原因是JsonValue
有错误
考虑以下类别:
public class Foo
{
public static implicit operator short (Foo value)
{
return 0;
}
public static implicit operator bool (Foo value)
{
return false;
}
}
在选择合适的方法时,编译器可以将该类视为Foo
、对象和short
以及bool。这就是为什么会出现错误
以下方法或属性之间的调用不明确:
“条形图(对象)”和“条形图(短)”
谢谢你帮了我这么多
public class Foo
{
public static implicit operator short (Foo value)
{
return 0;
}
public static implicit operator bool (Foo value)
{
return false;
}
}
Foo f = new Foo();
Bar(f);