我已经浏览了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 并显示在屏幕上。
我在这里错过了什么?
提前谢谢。
好吧,实际上,对于我第一眼看到的,您在注释掉的代码中使用了 timeable1 集合,您说有效的代码。我想你不需要
timelist.DataContext = timetable1;
代码行,注释掉并以这种方式设置下一行:
timelist.ItemsSource = timeable1;
如果您尝试显示一个简单的列表,而不是分组列表,则可以使用集合设置 longlistselector.itemsource 属性。在您的 xaml 文件上,一切看起来都很好。
希望有帮助。