【问题标题】:Display images from Assets with JSON - Windows Phone 8使用 JSON 显示来自资产的图像 - Windows Phone 8
【发布时间】:2014-11-16 22:32:56
【问题描述】:

我正在制作一个 WP8 应用程序,在我的 Assets 文件夹中有很多本地内容。所以我使用的是存储在 JSON 文件夹中的 JSON 文件。

我已经能够非常轻松地将 JSON 解析为 C#,现在我正尝试在列表中显示数据。我显示标题没有问题,但我无法显示图像,即使我得到了文件名。

我的图片存储在“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>

还有我的 C# 页面

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

有什么想法吗?

【问题讨论】:

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


    【解决方案1】:

    代码应该可以工作。唯一可能发生的问题是您的 ListBox 数据绑定定义不正确。

    我没有看到任何 .ItemsSource =ItemsSource={Binding some_collection}

    另一件事是确保photo.filename 返回正确的文件。

    设置字符串 debug_string = "Assets/Content/" + photo.filename + ".jpg";

    确保一切正确。


    最后一件事是确保文件实际上是在项目内的资产文件夹中

    BuildAction 设置为Content

    像这样

    【讨论】:

    • 是的,这最后一步花了我几个小时 =)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多