异步加载图像

发布于 2025-01-03 18:30:07 字数 3249 浏览 4 评论 0原文

我想用最好的方式在我的 PanoramaPage 中显示图像。我下载一个页面并显示它的信息,然后我想异步加载另一个带有图像的页面。所以我使用 HttpWebRequest 并得到响应。一切都很好,希望这是最好的方式。因此,我创建了 GaleryViewModel,并为页面上的所有图像添加了 url 到我的类中。 但有一个问题。我看不到视图中的图像。这是我的观点:

<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; }
 ...
 }

我不确定我做错了什么,但我认为这是绑定的问题。感谢您的帮助

I want to use best way to show images in my PanoramaPage. I download one page and shows it´s information a then I want to async load another page with images. So I am using HttpWebRequest and I get response. Everything is okay and hope this is best way for these. So I create my GaleryViewModel and for all images at page I add url to my class.
And there is a problem. I can´t see images in view. This i my view:

<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>

This is content of my WebResponse event handler:

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;

and for all GaleryViewModel and 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; }
 ...
 }

I am not sure what I am doing wrong but I think that is something with binding. Thanks for help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别挽留 2025-01-10 18:30:07

看看这个,您似乎还没有通知 MovieExt.Images 属性已更改。如果不这样做,您的 ListBox 将不会更新其 Items。为此,MovieExt 还需要支持 INotifyPropertyChanged

Looking at this it looks like you haven't notified that the MovieExt.Images property has changed. Without doing this your ListBox won't update its Items. To do this MovieExt will also need to support INotifyPropertyChanged.

三生殊途 2025-01-10 18:30:07

您必须将 Uri 转换为 BitmapImage,因为 BitmapImage UriSource 是一个流。请看一下:图像 UriSource 和数据绑定

You have to convert Uri to BitmapImage, because BitmapImage UriSource is a stream. Please take a look at this: Image UriSource and Data Binding

短叹 2025-01-10 18:30:07

将加载方法(仅用于测试)替换为:

MovieExt movie = new MovieExt();
movie.Images = new List<GaleryViewModel>();
GaleryViewModel fotka1 = new GaleryViewModel();
fotka1.LineOne = "line1";
fotka1.ImgURL = "http://proservice.kiev.ua/wp-content/uploads/2011/10/apple-logo.jpg";
GaleryViewModel fotka2 = new GaleryViewModel();
fotka2.LineOne = "line2";
fotka2.ImgURL = "http://www.mykhailenko.com/blog/wp-content/uploads/2010/12/apple-logo-history-2.png";
movie.Images.Add(fotka1);
movie.Images.Add(fotka2);
listImages.ItemsSource = movie.Images;

并且它有效

我猜问题出在这一行:

if (matches.Count > 0)

您没有匹配项,因此您的 Url 为空

您可以向我们提供您的服务返回的数据吗在您的上下文中调试代码?

另外,为什么您需要循环来完成此作业?

foreach (Match match in matches)
    fotka.ImgURL = match.Value;

Replace loading method (just for test) to:

MovieExt movie = new MovieExt();
movie.Images = new List<GaleryViewModel>();
GaleryViewModel fotka1 = new GaleryViewModel();
fotka1.LineOne = "line1";
fotka1.ImgURL = "http://proservice.kiev.ua/wp-content/uploads/2011/10/apple-logo.jpg";
GaleryViewModel fotka2 = new GaleryViewModel();
fotka2.LineOne = "line2";
fotka2.ImgURL = "http://www.mykhailenko.com/blog/wp-content/uploads/2010/12/apple-logo-history-2.png";
movie.Images.Add(fotka1);
movie.Images.Add(fotka2);
listImages.ItemsSource = movie.Images;

and it works

I guess the problem is in this line:

if (matches.Count > 0)

You have no matches, so your Url is empty

Can you provide us data your service return to debug code in your context?

Also, why you need loop for this assignment?

foreach (Match match in matches)
    fotka.ImgURL = match.Value;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文