我想用最好的方式在我的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