C# 使用Json键绑定图像源url
这是我的C# 使用Json键绑定图像源url,c#,json,windows-phone-8,data-binding,C#,Json,Windows Phone 8,Data Binding,这是我的XAML代码 <phone:PivotItem Header="Categories" Margin="12,0,12,8"> <ListBox x:Name="ImageList"> <ListBox.ItemTemplate> <DataTemplate> <Grid Width="420"> <StackPanel Hei
XAML代码
<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\u image
的值,如下所示:
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; }
}
JArray categories = (JArray)post["posts"];
类别的值如下所示
[
{
"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
绑定的帮助下动态显示图像。尝试更改代码,如下所示:
在下面的XAML
文件中
<ListBox x:Name="ImageList" DataContext="{Binding}">
编辑:
放入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>
<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>
放入.cs文件:
RootObject Item = JsonConvert.DeserializeObject<RootObject>(JStr);
DataContext = Item;
RootObject Item=JsonConvert.DeserializeObject(JStr);
DataContext=项目;
上次编辑:
放入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>
<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>
尝试按以下方式更改代码:
在下面的XAML
文件中
<ListBox x:Name="ImageList" DataContext="{Binding}">
编辑:
放入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>
<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>
放入.cs文件:
RootObject Item = JsonConvert.DeserializeObject<RootObject>(JStr);
DataContext = Item;
RootObject Item=JsonConvert.DeserializeObject(JStr);
DataContext=项目;
上次编辑:
放入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>
<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文件代码并更新我的答案检查它没有运气发现这一点,你能给我发送好教程(文档或视频)的链接,从那里我可以学到同样的东西。谢谢你的回复,当使用你更新的代码时,我收到一个例外,异常:在mscorlib.ni.dll中发生“System.Globalization.CultureNotFoundException”类型的第一次机会异常在System.Windows.ni.dll中发生“System.Globalization.CultureNotFoundException”类型的第一次机会异常存在相同的异常,我使用LongListSelector而不是ListBox。我更新XAML文件代码并更新我的答案是检查是否实施某种懒惰加载,否则你可能会有一个内存不足的例外。你能提供链接来学习相同的,因为我是初学者在Windows电话开发。考虑实现某种懒惰加载,否则,你可能会得到一个内存不足的例外,你可以提供链接来学习同样的内容,因为我是windows phone开发的初学者。