第一次来电,长期倾听……
我整天都在努力解决数据绑定问题,不知所措。我正在尝试使用 Windows Phone 8 的 LongListSelector 显示链接列表。
我有一个代表链接的类(外部链接)
我有一个代表链接集合的类(ExternalLinkList)
我已经验证 Collection 加载了正确的数据而没有问题。鉴于下面的代码,有人可以指出我确信这是我刚刚看到的一个令人难以置信的明显问题吗?
public class ExternalLink
{
/// <summary>
/// Property for link title
/// </summary>
public String Title { get; set; }
/// <summary>
/// Property for link text
/// </summary>
public String Text { get; set; }
/// <summary>
/// Property for link image name.
/// </summary>
public String ImageName { get; set; }
/// <summary>
/// Property for item text
/// </summary>
public String Link { get; set; }
public ExternalLink(string title, string text, string imagename, string link)
{
Title = title;
Text = text;
ImageName = imagename;
Link = link;
}
public ExternalLink()
{
// TODO: Complete member initialization
}
public Uri Url
{
get
{
Uri uri = new Uri(Link, UriKind.RelativeOrAbsolute);
return uri;
}
set
{
Url = value;
}
}
}
集合代码(更新):
public class ExternalLinkList
{
private ObservableCollection<ExternalLink> list;
public ObservableCollection<ExternalLink> List
{
get
{
if (list == null)
{
list = new ObservableCollection<ExternalLink>();
}
return list;
}
set
{
if (list != value)
{
list = value;
}
}
}
public ExternalLinkList()
{
}
}
视图模型代码:
public partial class LinkListPage : PhoneApplicationPage
{
public static ExternalLinkList List {get; set;}
public static Uri LinkListXML = new Uri("/GenericRssReader;component/pagelinklist.xml", UriKind.Relative);
public static string imagePathTo = "/GenericRssReader;Assets/LinkImages/";
public LinkListPage()
{
InitializeComponent();
Loaded += LinkListPage_Loaded;
}
private void LinkListPage_Loaded(object sender, RoutedEventArgs e)
{
List = new ExternalLinkList();
StreamResourceInfo xml = App.GetResourceStream(LinkListXML);
ParseOPML(xml.Stream, imagePathTo);
//MessageBox.Show("test " + List.List.Count);
LinkList.ItemsSource = List.List;
}
private static void ParseOPML(Stream stream, string pathto)
{
XDocument xDocument = XDocument.Load(stream);
// XML parsed using Linq
ExternalLink lin = null;
foreach(XElement outline in xDocument.Descendants("outline"))
{
string ImageName = pathto + outline.Attribute("imageName").Value;
lin = new ExternalLink(outline.Attribute("title").Value,
outline.Attribute("text").Value,
ImageName,
outline.Attribute("xmlUrl").Value);
List.List.Add(lin);
//MessageBox.Show(lin.Title + "\n" + lin.Text + "\n" + lin.ImageName + "\n" + lin.Link);
}
//MessageBox.Show("Title 0: " + List.List[0].Title + "\n ImageName 0: " + List.List[0].ImageName);
}
}
xml代码
<phone:LongListSelector
x:Name="LinkList"
HorizontalAlignment="Left"
Margin="5,0,0,0"
VerticalAlignment="Top"
DataContext="local:ExternalLink"
SelectionChanged="LinkList_SelectionChanged"
ItemTemplate="{StaticResource HorizontalTileDataTemplate}"
>
</phone:LongListSelector>