【问题标题】:Binding Image Source url with Json Key使用 Json Key 绑定图像源 url
【发布时间】:2015-10-21 12:23:59
【问题描述】:

这是我的XAML Code

<phone:PivotItem Header="Categories" Margin="12,0,12,8">
    <ListBox x:Name="ImageList">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid Width="420">
                    <StackPanel Height="325" VerticalAlignment="Top">
                        <Image x:Name="eventImage" Source="{Binding category_image}" VerticalAlignment="Top"/>
                    </StackPanel>
                </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
    </ListBox>
</phone:PivotItem>

在这里,我将图像与我在课堂 Category 中获取的变量绑定,如下所示:

public class Post
{
    public string post_id { get; set; }
    public string category { get; set; }
    public string category_image { get; set; }
}

public class RootObject
{
    public int success { get; set; }
    public string message { get; set; }
    public List<Post> posts { get; set; }
}

我得到category_image 的值是这样的:

 JArray categories = (JArray)post["posts"];

categories的值如下

[
    {
        "post_id": "5",
        "category": "Hospitals",
        "category_image": "http://right.mydomain.com/deal_img/hospitals.png"
    },
    {
        "post_id": "2",
        "category": "Play Schools",
        "category_image": "http://right.mydomain.com/deal_img/playschool.png"
    },
    {
        "post_id": "4",
        "category": "Fitness",
        "category_image": "http://right.mydomain.com/deal_img/gym.png"
    },
    {
        "post_id": "7",
        "category": " Salon & Spa",
        "category_image": "http://right.mydomain.com/deal_img/BEAUTY.png"
    },
    {
        "post_id": "12",
        "category": "Food & Drink",
        "category_image": "http://right.mydomain.com/deal_img/restaurants.png"
    },
    {
        "post_id": "13",
        "category": "Car Care",
        "category_image": "http://right.mydomain.com/deal_img/carwash.png"
    }
]

现在我被困在这里了,接下来我必须做什么,在 URL 绑定的帮助下动态显示图像。

【问题讨论】:

  • 考虑实现某种延迟加载,否则可能会出现内存不足异常
  • 能否提供链接学习一下,因为我是windows phone开发的初学者。

标签: c# json windows-phone-8 data-binding


【解决方案1】:

尝试更改代码如下:

XAML下面的文件中

<ListBox x:Name="ImageList" DataContext="{Binding}">

在代码端添加下面的代码

JArray categories = (JArray)post["posts"];
DataContext = categories

编辑:

放入 XAML:

<phone:Pivot x:Name="pivotItem">
    <phone:PivotItem Header="Categories" DataContext="{Binding}" FontSize="10">
        <ListBox x:Name="ImageList" ItemsSource="{Binding Path=posts}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Width="Auto">
                        <StackPanel Height="323" VerticalAlignment="Top">
                            <Image x:Name="Image" Source="{Binding Path=category_image}" 
                                   VerticalAlignment="Top" Height="323" />
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </phone:PivotItem>
</phone:Pivot>

放入.cs文件:

RootObject Item = JsonConvert.DeserializeObject<RootObject>(JStr);
DataContext = Item;

上次编辑:

放入 XAML:

<Grid x:Name="LayoutRoots" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <!--Pivot Control-->
        <phone:Pivot x:Name="pivotItem" Title="Test Category">          
            <!--Pivot item one -->
            <phone:PivotItem Header="Categories" DataContext="{Binding}" FontSize="10">
                <Grid>
                    <phone:LongListSelector x:Name="llsCategoriesList" Grid.Row="0"
                                            Background="Transparent"
                                            LayoutMode="List"
                                            IsGroupingEnabled="False"
                                            HideEmptyGroups="true"
                                            ItemsSource="{Binding Path=posts}">
                        <phone:LongListSelector.ItemTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <Grid Width="Auto">
                                        <StackPanel Height="323" VerticalAlignment="Top">
                                            <Image x:Name="Image" Source="{Binding Path=category_image}" 
                                                   VerticalAlignment="Top" Height="323" />
                                        </StackPanel>
                                    </Grid>
                                </StackPanel>
                            </DataTemplate>
                        </phone:LongListSelector.ItemTemplate>
                    </phone:LongListSelector>
                </Grid>
            </phone:PivotItem>
        </phone:Pivot>
    </Grid>
</Grid>

【讨论】:

  • 没有运气,你能不能把好的教程(文档或视频)的链接发给我,我可以从中学习。
  • 感谢您的回复,在使用您更新的代码时,我收到一个异常,异常:在 mscorlib.ni.dll 中发生“System.Globalization.CultureNotFoundException”类型的第一次机会异常第一次机会异常System.Windows.ni.dll 中发生了“System.Globalization.CultureNotFoundException”类型的相同异常,我使用 LongListSelector 而不是 ListBox。
  • 我更新 XAML 文件代码并更新我的答案检查它
猜你喜欢
  • 2018-11-01
  • 1970-01-01
  • 2014-05-18
  • 1970-01-01
  • 2016-09-15
  • 1970-01-01
  • 2010-10-19
  • 1970-01-01
  • 2016-12-30
相关资源
最近更新 更多