简体   繁体   English

使用JSON显示资产中的图像-Windows Phone 8

[英]Display images from Assets with JSON - Windows Phone 8

I am making a WP8 application with a lot of local content in my Assets folder. 我正在使用Assets文件夹制作具有很多本地内容的WP8应用程序。 So I am using a JSON file stored in the a JSON folder. 所以我正在使用存储在JSON文件夹中的JSON文件。

I have been able to parse the JSON to C# very easily and now I am trying to display the data in a list. 我已经能够非常轻松地将JSON解析为C#,现在我试图在列表中显示数据。 I had no problem with displaying the title but I am unable to display an image, even with the filename I am got. 显示标题没有问题,但是即使有文件名,也无法显示图像。

My images are stored in "Assets/Content/mediaXXX.jpg"; 我的图像存储在“资产/内容/mediaXXX.jpg”中;

ListViewModel : ListViewModel:

 public class ListViewModel
    {
        public string Title { get; set; }
        public string Subtitle { get; set; }
        public BitmapImage ListImage { get; set; }
    }

XAML XAML

 <ListBox Margin="0,1,0,0"
                 Height="730"
                 x:Name="MainList">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Height="120"
                      Width="480">
                        <StackPanel Orientation="Horizontal">
                            <Image HorizontalAlignment="Left"
                               Source="{Binding ListImage}"
                               Margin="12"
                               Stretch="UniformToFill"
                               Width="130"/>
                            <Grid>
                                <TextBlock x:Name="ListItemTitle"
                                           Text="{Binding Title}"/>
                                <TextBlock x:Name="ListItemSubTitle"
                                       Text="{Binding Subtitle}"/>
                            </Grid>
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

And my C# page 还有我的C#页面

    BitmapImage image = new BitmapImage(new Uri(@"Assets/Content/" + photo.filename + ".jpg", UriKind.Relative);
    l.ListImage = image;

Any idea? 任何想法?

Code should work. 代码应该起作用。 Only problems that might occur is your ListBox databinding is incorrectly defined. 仅可能发生的问题是您的ListBox数据绑定定义不正确。

I don't see any .ItemsSource = or ItemsSource={Binding some_collection} 我没有看到任何.ItemsSource =ItemsSource={Binding some_collection}

Another thing is make sure that photo.filename is returning the correct file. 另一件事是确保photo.filename返回正确的文件。

Set a string debug_string = "Assets/Content/" + photo.filename + ".jpg"; 设置一个字符串debug_string =“ Assets / Content /” + photo.filename +“ .jpg”;

Make sure everything is correct. 确保一切正确。


Last thing is to make sure the files are actually in the Assets Folder inside the project and its 最后一件事是确保文件实际上位于项目及其内部的Assets文件夹中

BuildAction is set to Content BuildAction设置为Content

like so 像这样

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM