WP7 将文本和图像绑定到中心磁贴

本文关键字:绑定 文本 图像 WP7 | 更新日期: 2023-09-27 18:30:50

我正在尝试将匹配的名称和图像绑定到中心磁贴中。 数据来自输出 JSON 的 API。我在反序列化数据并使用 foreach 循环将数据添加到我的类中没有问题。这是设置:

    // Deserialize home page data
    public void jsonHome_GetDataCompleted(object snder, DownloadStringCompletedEventArgs e)
    {
        NewReleasesCharts homeData = JsonConvert.DeserializeObject<NewReleasesCharts>(e.Result);
        foreach (FeaturedReleases release in homeData.results.featuredReleases)
        {
            int releaseID = release.id;
            string releaseName = release.name;
            string releaseImg = release.images.large.url;
            new ReleaseLarge()
            {
                url = releaseImg
            };
            new FeaturedReleases()
            {
                id = releaseID,
                name = releaseName
            };
        }
        this.listRelease.ItemsSource = homeData.results.featuredReleases;        
    }

这是我的课程

public class NewReleasesCharts 
{
    //public Metadata metadata { get; set; }
    public ResultHome results = new ResultHome();
    public IEnumerator<ResultHome> GetEnumerator()
    {
        return this.results.GetEnumerator();
    }
}
public class ResultHome
{
    public List<FeaturedReleases> featuredReleases { get; set; }
    //public List<FeaturedCharts> featuredCharts { get; set; }
    //public List<TopDownloads> topdownloads { get; set; }
    //public List<MostPopularReleases> mostPopularReleases { get; set; }
    //public List<Components> components { get; set; }
    internal IEnumerator<ResultHome> GetEnumerator()
    {
        throw new NotImplementedException();
    }
}
public class FeaturedReleases
{
    public int id { get; set; }
    public string type { get; set; }
    public string name { get; set; }
    public string slug { get; set; }
    public ReleaseImage images { get; set; }
}
public class ReleaseImage
{
    //public ReleaseSmall small { get; set; }
    public ReleaseMedium medium { get; set; }
    public ReleaseLarge large { get; set; }
}
public class ReleaseLarge
{
    public int width { get; set; }
    public int height { get; set; }
    public string url { get; set; }
    public string secureUrl { get; set; }
}

和我的 XAML

                    <ListBox Grid.Row="0" x:Name="listRelease">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <toolkit:WrapPanel Orientation="Horizontal" />
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <toolkit:HubTile Title="{Binding name}" Source="{Binding url}" Margin="10" />
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

目前,只有name显示在中心磁贴中。我认为这是因为名称和图像在不同的类中,但我无法找到解决方案。感谢您的帮助。

WP7 将文本和图像绑定到中心磁贴

你有

Source="{Binding img}"

但是在您的模型中,没有任何地方有一个名为 img 的公共属性。

我要出去走,但如果有什么不同的话,你的绑定应该是这样的:

Source="{Binding images.medium.url}"