【问题标题】:Map a JSON array to a LongListSelector of Images将 JSON 数组映射到图像的 LongListSelector
【发布时间】:2015-01-25 12:22:20
【问题描述】:

我最近开始在 C#/XAML 中为 Windows Phone 8 应用程序处理 JSON 数据。 到目前为止,我已经成功地将简单的 JSON 数据类型解析并映射到 LongListSelector。即使在阅读了很多小时的答案和帖子之后,我还没有解决这个特定的问题。

现在我遇到了这样的 JSON:

{
    "images":
    [
        "http://motoroids.com/Honda-Logo.png",
        "htt//somelink0/1.png",
        "htt//somelink1/2.png",
        "htt//somelink2/3.png"
    ]
}

我想在 LongListSelector 中显示这些图像,定义如下:

<phone:LongListSelector Name="sponsorlist" ItemsSource="{Binding images}" HorizontalAlignment="Left" Height="504" Margin="10,10,0,0" VerticalAlignment="Top" Width="436">
                <phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="10 10 10 10">
                            <Image Source="{Binding ????}" Height="219" Width="153" />
                        </StackPanel>
                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
            </phone:LongListSelector>

这是 JSON 数据的类:

public class RootObject
        {
            public List<string> images { get; set; }
        }

为了解析它,我使用 Json.Net nuget 包,如下所示:

var sponsfile = ReadFile(@"Assets/sponsors.txt");
RootObject obj = JsonConvert.DeserializeObject<RootObject>(sponsfile);
sponsorlist.DataContext = obj;

我不明白应该写什么来代替 ????显示图像。 这是我的第一个问题,请指出任何缺陷,如果需要,我会尽力提供更多信息。

【问题讨论】:

    标签: c# json windows-phone-8 json.net json-deserialization


    【解决方案1】:

    你只需要绑定当前元素。 所以只需编辑图片绑定如下:

    <Image Source="{Binding}" Height="219" Width="153" />
    

    【讨论】:

      猜你喜欢
      • 2017-02-24
      • 2019-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多