简体   繁体   中英

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. So I am using a JSON file stored in the a JSON folder.

I have been able to parse the JSON to C# very easily and now I am trying to display the data in a list. 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";

ListViewModel :

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

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

    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.

I don't see any .ItemsSource = or ItemsSource={Binding some_collection}

Another thing is make sure that photo.filename is returning the correct file.

Set a string 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

BuildAction is set to Content

like so

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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