我想用最好的方式在我的PanoramaPage中显示图像。我下载了一个页面,并显示它的信息,然后我想异步加载另一个页面的图像。所以我使用HttpWebRequest
,我得到了回应。一切都很好,希望这是最好的方式。因此,我创建了我的GaleryViewModel,并在页面上为所有图像添加了网址到我的课程中。 还有一个问题。我无法看到图像。这我我的观点:异步加载图像
<ListBox ItemsSource="{Binding Images}" x:Name="listImages" Height="652" Canvas.Top="80">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,17">
<Image Height="100" Width="100" Margin="12,0,9,0" >
<Image.Source>
<BitmapImage UriSource="{Binding ImgURL}" CreateOptions="BackgroundCreation"/>
</Image.Source>
</Image>
<TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这是我WebResponse的事件处理程序的内容:
MovieExt movie = this.DataContext as MovieExt;
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(response);
var photos = from ti in doc.DocumentNode.Descendants("div")
where ti.Attributes["class"] != null && ti.Attributes["class"].Value == "photo"
select ti;
Regex rgx = new Regex("http://[0-9a-zA-Z_./]+");
foreach (var photo in photos)
{
GaleryViewModel fotka = new GaleryViewModel();
string style = photo.Attributes["style"].Value;
MatchCollection matches = rgx.Matches(style);
if (matches.Count > 0)
{
foreach (Match match in matches)
fotka.ImgURL = match.Value;
}
fotka.LineOne = "Test";
movie.Images.Add(fotka);
}
this.DataContext = movie;
this.listImages.ItemsSource = movie.Images;
并为所有GaleryViewModel和MovieExt:
public class GaleryViewModel : INotifyPropertyChanged
{
private string _imgUrl;
public string ImgURL
{
get
{
return _imgUrl;
}
set
{
if (value != _imgUrl)
{
_imgUrl = value;
NotifyPropertyChanged("ImgURL");
}
}
}
private string _lineOne;
public string LineOne
{
get
{
return _lineOne;
}
set
{
if (value != _lineOne)
{
_lineOne = value;
NotifyPropertyChanged("LineOne");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class MovieExt
{
public string Title { get; set; }
public string Year { get; set; }
public string Url { get; set; }
...
public List<GaleryViewModel> Images { get; set; }
...
}
我不知道我在做什么错了,但我认为这是有约束力的事情。感谢您的帮助
您是否曾尝试在'GaleryViewModel.ImgURL.get'中放置一个断点并查看是否命中?如果不是,那将表明有约束力的问题。 – 2012-02-07 11:15:43
我试了一下,我已经打了第一次,然后多次获得(为我添加的所有图像)和最后获得itemssource。 – 2012-02-07 12:30:03
movie.Images.Add中的这部电影是什么? 它是observableCollection吗? 而不是使用列表<>你应该使用ObservableCollection <> http://www.codeproject.com/Articles/42536/List-vs-ObservableCollection-vs-INotifyPropertyCha – 2012-02-14 21:15:10