【问题标题】:Cannot get data to bind and display - Windows Phone 8 longlist selector and XML无法获取要绑定和显示的数据 - Windows Phone 8 长列表选择器和 XML
【发布时间】:2014-01-10 03:24:22
【问题描述】:

我已经浏览了 MSDN 和 StackEchange 以及其他地方,但似乎找不到解决我的问题的神奇缺失步骤。我正在使用以下代码隐藏加载一些 XML 数据(时间表信息):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Resources;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using XML_Long_List.Resources;
using System.Xml.Linq;
using System.Collections.ObjectModel;

namespace XML_Long_List
{
public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();

        // Sample code to localize the ApplicationBar
        //BuildLocalizedApplicationBar();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {

        XElement loadedXEL;
        Collection<timetable> timetable1;

        StreamResourceInfo xml = Application.GetResourceStream(new Uri("XMLData/tt.xml", UriKind.Relative));
        loadedXEL = XElement.Load(xml.Stream);

        //retriving data from xml using LINQ
        var dataset = from query in loadedXEL.Descendants("record")
                      select new timetable
                      {
                          uniqueID = query.Element("uniqueid").Value,
                          departTime = query.Element("departtime").Value,
                          departAMPM = query.Element("departampm").Value
                      };
        timetable1 = new Collection<timetable>();
        foreach (timetable ttentry in dataset)
        {
            timetable1.Add(ttentry);
        }
        timelist.DataContext = timetable1;
        timelist.ItemsSource = dataset.ToList();

        /*
        foreach (var entryitem in timetable1)
        {
            if (String.IsNullOrEmpty(entryitem.uniqueID))
            {
                MessageBox.Show("NULL OR EMPTY");
            }
            else
            {
                MessageBox.Show(entryitem.uniqueID);
            }
            if (String.IsNullOrEmpty(entryitem.departTime))
            {
                MessageBox.Show("NULL OR EMPTY");
            }
            else
            {
                MessageBox.Show(entryitem.departTime);
            }
            if (String.IsNullOrEmpty(entryitem.departAMPM))
            {
                MessageBox.Show("NULL OR EMPTY");
            }
            else
            {
                MessageBox.Show(entryitem.departAMPM);
            }
        }
        */

    }
}

public class timetable
{
    private string _uniqueID;
    private string _departTime;
    private string _departAMPM;

    public string uniqueID { get { return _uniqueID; } set { _uniqueID = value; } }
    public string departTime { get { return _departTime; } set { _departTime = value; } }
    public string departAMPM { get { return _departAMPM; } set { _departAMPM = value; } }
}
}

XAML 是:

<phone:PhoneApplicationPage
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:XML_Long_List"
x:Class="XML_Long_List.MainPage"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<phone:PhoneApplicationPage.Resources>
    <ControlTemplate x:Key="LongListSelectorControlTemplate1" TargetType="phone:LongListSelector">
        <Grid Margin="0,0,0,275">
            <Grid.RowDefinitions>
                <RowDefinition Height="14*"/>
                <RowDefinition Height="15*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="51*"/>
                <ColumnDefinition Width="101*"/>
            </Grid.ColumnDefinitions>
            <TextBlock TextWrapping="Wrap" Text="{Binding departAMPM}" Foreground="White">
                <TextBlock.DataContext>
                    <local:timetable/>
                </TextBlock.DataContext>
            </TextBlock>
            <TextBlock Grid.Column="1" TextWrapping="Wrap" Text="{Binding departTime}"/>
            <Button Content="{Binding uniqueID}" HorizontalAlignment="Left" Grid.Row="1" VerticalAlignment="Top" Height="90" Width="153"/>
            <Button Content="{Binding departAMPM}" Grid.Column="1" HorizontalAlignment="Left" Grid.Row="1" VerticalAlignment="Top" Height="90" Width="303"/>
        </Grid>
    </ControlTemplate>
</phone:PhoneApplicationPage.Resources>

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

<!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
        <TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <phone:LongListSelector x:Name="timelist" HorizontalAlignment="Left" Height="449" VerticalAlignment="Top" Width="456" Template="{StaticResource LongListSelectorControlTemplate1}">
        </phone:LongListSelector>
    </Grid>

</Grid>

</phone:PhoneApplicationPage>

对不起,如果我的代码格式不是很好 - 在这里没有发布太多。

XML 是:

<?xml version="1.0" encoding="ISO-8859-1"?>
<meadinkent>
<record>
<uniqueid>1 </uniqueid>
<departtime>30 Dec 99</departtime>
<departampm>am</departampm>
<arrivetime>30 Dec 99</arrivetime>
<arriveampm>am</arriveampm>
</record>
<record>
<uniqueid>2 </uniqueid>
<departtime>30 Dec 99</departtime>
<departampm>am</departampm>
<arrivetime>30 Dec 99</arrivetime>
<arriveampm>am</arriveampm>
</record>
</record>
</meadinkent>

实际上有更多的记录,但格式都是一样的。

数据肯定是从 XML 文件正确加载的,因为我可以使用注释掉的代码显示它。然而,无论我尝试什么,我都无法让它填充 LongListSelector 并显示在屏幕上。

我在这里错过了什么?

提前致谢。

【问题讨论】:

    标签: xml data-binding windows-phone-8 longlistselector


    【解决方案1】:

    好吧,就我第一眼看到的情况而言,您在注释掉的代码中使用了 timeable1 集合,您说的代码有效。 我想你不需要

    timelist.DataContext = timetable1;
    

    代码行,将其注释掉并以这种方式设置下一行:

    timelist.ItemsSource = timeable1;
    

    如果您尝试显示一个简单列表,而不是分组列表,您可以将 longlistselector.itemsource 属性设置为一个集合。 您的 xaml 文件上的一切看起来都不错。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-30
      • 1970-01-01
      • 2013-07-30
      • 1970-01-01
      • 2016-05-02
      相关资源
      最近更新 更多