C# 使用带有搜索参数的组合框
大约一个小时前,我问了一个问题,问我如何做类似的事情,在我得到一些帮助后,我能够做到。基本上,我在说 “无法确定条件表达式的类型,因为'string'和'UltimateTeam.Toolkit.Parameter.Formation'之间没有隐式转换” 下面是代码,搜索一名球员:我还得到了队形上的错误C# 使用带有搜索参数的组合框,c#,C#,大约一个小时前,我问了一个问题,问我如何做类似的事情,在我得到一些帮助后,我能够做到。基本上,我在说 “无法确定条件表达式的类型,因为'string'和'UltimateTeam.Toolkit.Parameter.Formation'之间没有隐式转换” 下面是代码,搜索一名球员:我还得到了队形上的错误 public async void start() { var searchRequest = new SearchRequest();
public async void start()
{
var searchRequest = new SearchRequest();
var searchParameters = new PlayerSearchParameters
{
Formation = comboBox2.SelectedItem == null ? Formation.FourThreeThree : (Formation)(comboBox2.SelectedItem as ComboboxItem2).Value2,
};
第二段代码:
foreach (Formation formation in Enum.GetValues(typeof(Formation)))
{
ComboboxItem2 item2 = new ComboboxItem2();
item2.Text2 = formation.ToString();
item2.Value2 = formation;
comboBox2.Items.Add(item2);
}
public class ComboboxItem2
{
public string Text2 { get; set; }
public object Value2 { get; set; }
public override string ToString()
{
return Text2;
}
}
最后一段代码:
foreach (Formation formation in Enum.GetValues(typeof(Formation)))
{
ComboboxItem2 item2 = new ComboboxItem2();
item2.Text2 = formation.ToString();
item2.Value2 = formation;
comboBox2.Items.Add(item2);
}
public class ComboboxItem2
{
public string Text2 { get; set; }
public object Value2 { get; set; }
public override string ToString()
{
return Text2;
}
}
有什么办法可以解决吗
谢谢
杰克。用于将字符串
转换为枚举类型格式
Formation = comboBox2.SelectedItem == null
? Formation.FourThreeThree
: (Formation) Enum.Parse(typeof(Formation), comboBox2.Text);
编辑因此玩家搜索参数。编队
是一个字符串
,n这应该起作用:
Formation = comboBox2.SelectedItem == null
? Formation.FourThreeThree.ToString()
: comboBox2.Text,
在哪一行你得到了错误,上面写着“Formation=comboBox2.SelectedItem==null?Formation.FourThreeThree:(Formation)(comboBox2.SelectedItem作为ComboboxItem2.Value2),“它需要以一个“,”结尾,我想我不能这样做。它需要以“,”结尾,因为你需要选择更多的参数。我刚刚使用了它,得到了相同的错误,这里有一个屏幕上限:@user2382644:What type is
PlayerSearchParameters.Formation
?我假设它是格式
,但现在我认为它是一个字符串。然后,您可以将该类型更改为Formation
,或者使用字符串表示法,如comboBox2.Text
或的“fourthree”
。使用另一个参数尝试后,它就工作了。我不认为这是代码的问题(我想这是一个字符串。它在我添加的库中,它说这是一个常量字符串。