我想在ComboBox中的“设置名称”之前显示“设置图像”,但是我用来获取数据的API没有给我一个URL来获取图像,所以我必须创建这是我自己的。然而它确实提供了gatherercode,这就是我需要得到的正确图像:
Public string SetImageUrl = String.Format("http://gatherer.wizards.com/Handlers/Image.ashx?type=symbol&set={0}&size={1}&rarity={2}", sets.gatherercode, setImageSize, setImageRarity)
但是如果我在MainPage.xaml.cs中将setImageUrl绑定到图像源,当我将itemsource设置为MagicSets并将DataType设置为不同的命名空间(appName.Api而不是appName)时,该如何绑定。或者我如何创建自己的:
[DataMember(Name = "setImageUrl")]
public string setImageUrl{ get; set; }
将所有值作为上述字符串。
我现在拥有的代码的结果是我得到一个组合框,每个组中有一个项目,其中有一个textblox显示set.name
XAML代码
<StackPanel Orientation="Horizontal">
<ComboBox Name="SetComboBox" ItemsSource="{x:Bind MagicSets}">
<ComboBox.ItemTemplate>
<DataTemplate x:DataType="data:Set">
<StackPanel Orientation="Horizontal">
<Image source="{Binding imageUrl}"/>
<TextBlock Text="{x:Bind name}"/>
</Stackpanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
SetDataWrapper Code
[DataContract]
public class Set
{
[DataMember(Name = "code")]
public string code { get; set; }
[DataMember(Name = "name")]
public string name { get; set; }
[DataMember(Name = "type")]
public string type { get; set; }
[DataMember(Name = "border")]
public string border { get; set; }
[DataMember(Name = "mkm_id")]
public int mkm_id { get; set; }
[DataMember(Name = "booster")]
public IList<object> booster { get; set; }
[DataMember(Name = "mkm_name")]
public string mkm_name { get; set; }
[DataMember(Name = "releaseDate")]
public string releaseDate { get; set; }
[DataMember(Name = "gathererCode")]
public string gathererCode { get; set; }
[DataMember(Name = "magicCardsInfoCode")]
public string magicCardsInfoCode { get; set; }
[DataMember(Name = "block")]
public string block { get; set; }
[DataMember(Name = "oldCode")]
public string oldCode { get; set; }
[DataMember(Name = "onlineOnly")]
public bool? onlineOnly { get; set; }
}
[DataContract]
public class SetDataWrapper
{
[DataMember(Name = "sets")]
public IList<Set> sets { get; set; }
}
}
主页代码
public ObservableCollection<Card> MagicCards { get; set; }
public ObservableCollection<Set> MagicSets { get; set; }
public MainPage()
{
this.InitializeComponent();
MagicCards = new ObservableCollection<Card>();
MagicSets = new ObservableCollection<Set>();
}
private async void Page_Loaded(object sender, RoutedEventArgs e)
{
ProgressRingToggle(true);
await MagicFacade.PopulateMagicCardsAsync(MagicCards);
await MagicFacade.PopulateMagicSetsAsync(MagicSets);
ProgressRingToggle(false);
}
答案 0 :(得分:1)
我如何创建自己的:[DataMember(Name =“setImageUrl”)] public string setImageUrl {get;组;这将所有值都作为上面的字符串。
您可以将构造函数添加到Set
类。并在构造函数中设置setImageUrl
参数,如下所示:
[DataContract]
public class Set
{
public Set(string ImageSize, string setImageRarity)
{
setImageUrl = String.Format("http://gatherer.wizards.com/Handlers/Image.ashx?type=symbol&set={0}&size={1}&rarity={2}", gathererCode, ImageSize, setImageRarity);
}
[DataMember(Name = "setImageUrl")]
public string setImageUrl { get; set; }
[DataMember(Name = "code")]
public string code { get; set; }
[DataMember(Name = "name")]
public string name { get; set; }
[DataMember(Name = "type")]
...
在实例化类时设置ImageSize
和setImageRarity
,如下所示:
MagicSets = new ObservableCollection<Set>() {
new Set("medium","Rarity1"){name="suntest1",gathererCode="code"},
new Set("medium","Rarity2"){name="suntest2",gathererCode="code"}
};
之后,您可以将setImageUrl
与name
绑定。
<ComboBox.ItemTemplate>
<DataTemplate x:DataType="local:Set">
<StackPanel Orientation="Horizontal">
<Image Source="{x:Bind setImageUrl}"/>
<TextBlock Text="{x:Bind name}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>