数据未显示在Itemview页面中

时间:2015-04-04 18:06:26

标签: windows-phone-8

我正在尝试为Windows 8.1的Hub应用程序实现以下数据模型:

namespace TrialAPP.Data
{
public class SamplePoemItem
{

    public SamplePoemItem(String poemId, String poemtitle, String poemmean)
    {
        this.PoemId = poemId;
        this.PoemTitle = poemtitle;
        this.PoemMean = poemmean;
    }
    public string PoemId { get; private set; }
    public string PoemTitle { get; private set; }
    public string PoemMean { get; private set; }
    public override string ToString()
    {
        return this.PoemTitle;
    }

}

/// <summary>
/// Generic item data model.
/// </summary>
public class SampleDataItem
{
    public SampleDataItem(String uniqueId, String title, String subtitle, String imagePath, String description, String content)
    {
        this.UniqueId = uniqueId;
        this.Title = title;
        this.Subtitle = subtitle;
        this.Description = description;
        this.ImagePath = imagePath;
        this.Content = content;
        this.Poems = new ObservableCollection<SamplePoemItem>();


    }

    public string UniqueId { get; private set; }
    public string Title { get; private set; }
    public string Subtitle { get; private set; }
    public string Description { get; private set; }
    public string ImagePath { get; private set; }
    public string Content { get; private set; }
    public ObservableCollection<SamplePoemItem> Poems { get; private set; }

    public override string ToString()
    {
        return this.Title;
    }
}

/// <summary>
/// Generic group data model.
/// </summary>
public class SampleDataGroup
{
    public SampleDataGroup(String uniqueId, String title, String subtitle, String imagePath, String description)
    {
        this.UniqueId = uniqueId;
        this.Title = title;
        this.Subtitle = subtitle;
        this.Description = description;
        this.ImagePath = imagePath;
        this.Items = new ObservableCollection<SampleDataItem>();
    }

    public string UniqueId { get; private set; }
    public string Title { get; private set; }
    public string Subtitle { get; private set; }
    public string Description { get; private set; }
    public string ImagePath { get; private set; }
    public ObservableCollection<SampleDataItem> Items { get; private set; }

    public override string ToString()
    {
        return this.Title;
    }
}

/// <summary>
/// Creates a collection of groups and items with content read from a static json file.
/// 
/// SampleDataSource initializes with data read from a static json file included in the 
/// project.  This provides sample data at both design-time and run-time.
/// </summary>
public sealed class SampleDataSource
{
    private static SampleDataSource _sampleDataSource = new SampleDataSource();

    private ObservableCollection<SampleDataGroup> _groups = new ObservableCollection<SampleDataGroup>();
    private ObservableCollection<SamplePoemItem> _poems = new ObservableCollection<SamplePoemItem>();
    public ObservableCollection<SampleDataGroup> Groups
    {
        get { return this._groups; }
    }
    public ObservableCollection<SamplePoemItem> Poems
    {
        get { return this._poems; }
    }

    public static async Task<IEnumerable<SampleDataGroup>> GetGroupsAsync()
    {
        await _sampleDataSource.GetSampleDataAsync();

        return _sampleDataSource.Groups;
    }

    public static async Task<IEnumerable<SamplePoemItem>> GetPoemsAsync()
    {
        await _sampleDataSource.GetSampleDataAsync();

        return _sampleDataSource.Poems;


    }
    public static async Task<SampleDataGroup> GetGroupAsync(string uniqueId)
    {
        await _sampleDataSource.GetSampleDataAsync();
        // Simple linear search is acceptable for small data sets
        var matches = _sampleDataSource.Groups.Where((group) => group.UniqueId.Equals(uniqueId));
        if (matches.Count() == 1) return matches.First();
        return null;
    }

    public static async Task<SampleDataItem> GetItemAsync(string uniqueId)
    {
        await _sampleDataSource.GetSampleDataAsync();
        // Simple linear search is acceptable for small data sets
        var matches = _sampleDataSource.Groups.SelectMany(group => group.Items).Where((item) => item.UniqueId.Equals(uniqueId));
        if (matches.Count() == 1) return matches.First();
        return null;
    }

    public static async Task<SamplePoemItem> GetPoemAsync(string poemid)
    {
        await _sampleDataSource.GetSampleDataAsync();
        //Simple linear search for Poem data sets
        //  var matches = _sampleDataSource.Poems.SelectMany(item => item.PoemTitle).Where((poemtitle) => poemid.Equals(poemid));
        var matches = _sampleDataSource.Poems.Where((poemtitle) => poemid.Equals(poemid));
        if (matches.Count() == 1) return matches.First();
        return null;


    }

    private async Task GetSampleDataAsync()
    {
        if (this._groups.Count != 0)
            return;

        Uri dataUri = new Uri("ms-appx:///DataModel/SampleData.json");

        StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(dataUri);
        string jsonText = await FileIO.ReadTextAsync(file);
        JsonObject jsonObject = JsonObject.Parse(jsonText);
        JsonArray jsonArray = jsonObject["Groups"].GetArray();

        foreach (JsonValue groupValue in jsonArray)
        {
            JsonObject groupObject = groupValue.GetObject();
            SampleDataGroup group = new SampleDataGroup(groupObject["UniqueId"].GetString(),
                                                        groupObject["Title"].GetString(),
                                                        groupObject["Subtitle"].GetString(),
                                                        groupObject["ImagePath"].GetString(),
                                                        groupObject["Description"].GetString());

            foreach (JsonValue itemValue in groupObject["Items"].GetArray())
            {
                JsonObject itemObject = itemValue.GetObject();
                group.Items.Add(new SampleDataItem(itemObject["UniqueId"].GetString(),
                                                   itemObject["Title"].GetString(),
                                                   itemObject["Subtitle"].GetString(),
                                                   itemObject["ImagePath"].GetString(),
                                                   itemObject["Description"].GetString(),
                                                   itemObject["Content"].GetString()));

                foreach (JsonValue poemValue in itemObject["Poems"].GetArray())
                {
                    JsonObject poemObject = poemValue.GetObject();
                 Poems.Add(new SamplePoemItem(poemObject["PoemId"].GetString(),
                        poemObject["PoemTitle"].GetString(),
                        poemObject["PoemMean"].GetString()));
                }
            }
            this.Groups.Add(group);
        }
    }
}

}

此模型的数据如下:

{
"Groups": [
    {
        "UniqueId": "Group-1",
        "Title": "Group Title: 1",
        "Subtitle": "Group subtitle: 1",
        "ImagePath": "Assets/DarkGray.png",
        "Description": "Group Description: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus tempor scelerisque lorem in vehicula. Aliquam tincidunt, lacus ut sagittis tristique, turpis massa volutpat augue, eu rutrum ligula ante a ante",
        "Items": [
            {
                "UniqueId": "Group-1-Item-1",
                "Title": "Item Title: 1",
                "Subtitle": "Item Subtitle: 1",
                "ImagePath": "Assets/LightGray.png",
                "Description": "Item Description:",
                "Content": "",
                "Poems": [
                    {
                        "PoemId": "PoemId",
                        "PoemTitle": "PoemTitle",
                        "PoemMean": "PoemMean"

                    },
                    {
                        "PoemId": "PoemId1",
                        "PoemTitle": "PoemTitle2",
                        "PoemMean": "PoemMean2"
                    }
                ]
            }

        ]
    }

]

}

这是我在显示数据的ItemPage.Xaml。

                                                                                                            

<Grid x:Name="LayoutRoot">
    <Grid.ChildrenTransitions>
        <TransitionCollection>
            <EntranceThemeTransition/>
        </TransitionCollection>
    </Grid.ChildrenTransitions>

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!-- Title Panel -->
    <StackPanel Grid.Row="0" Margin="19,0,0,0">
        <TextBlock x:Uid="Header" Text="application name" Style="{ThemeResource TitleTextBlockStyle}" Margin="0,12,0,0" />
        <TextBlock d:DataContext="{Binding Groups[0].Items[0], Source={d:DesignData Source=../TrialAPP.Shared/DataModel/SampleData.json, Type=data:SampleDataSource}}" Text="{Binding Title}" Style="{ThemeResource HeaderTextBlockStyle}" Margin="0,-6.5,0,26.5" CharacterSpacing="{ThemeResource PivotHeaderItemCharacterSpacing}"/>
    </StackPanel>

    <!--TODO: Content should be placed within the following grid 
              to show details for the current item -->
    <Grid Grid.Row="1" x:Name="ContentRoot" Margin="19,9.5,19,0">
        <ListView
        x:Name="PoemListView"
        AutomationProperties.AutomationId="PoemListView"
        AutomationProperties.Name="Poems In Item"
        TabIndex="1"
        Grid.Row="1"
        ItemsSource="{Binding Poems}"
        IsItemClickEnabled="True"
        ItemClick="PoemView_PoemClick"
        SelectionMode="None"
        IsSwipeEnabled="False"
        Margin="19,0,0,0">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>

                        <StackPanel Grid.Column="1" VerticalAlignment="Top" Margin="15,0,-295.833,-229" Height="312">
                            <TextBlock Text="{Binding PoemId}" Style="{ThemeResource ListViewItemTextBlockStyle}"/>
                            <TextBlock Text="{Binding PoemTitle}" Style="{ThemeResource ListViewItemContentTextBlockStyle}" Foreground="{ThemeResource PhoneMidBrush}"/>
                            <TextBlock Text="{Binding PoemMean}" Style="{ThemeResource ListViewItemSubheaderTextBlockStyle}"/>
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Grid>

问题是ItemPage.Xaml在预览中显示完美。但是当我部署应用程序时,我在ItemPage.Xaml中没有任何内容。

我做错了什么?在构建期间我没有收到任何错误/警告。

0 个答案:

没有答案