【问题标题】:Image control not refreshed in windows phone appWindows Phone 应用程序中未刷新图像控件
【发布时间】:2013-09-18 11:55:03
【问题描述】:

我的应用中有一个图像列表,它们都在一个列表框中,我将源作为 URL 提供。

一切正常,可以显示来自我提供的 URL 的图像。

当我更改图像并重新加载我的ListBox 时,该更改并未反映在该图像上,它显示的是旧图像而不是新图像。

我确定,在图像更改后,我将新图像 URL 绑定到源,但它显示旧图像,我无法找到为什么会发生这种情况,这是我的代码

  <ListBox Grid.Row="1" HorizontalAlignment="Left" Margin="0,81,0,0" Name="wishListListBox" VerticalAlignment="Top" Width="480" Visibility="Visible">
        <ListBox.ItemTemplate>
            <DataTemplate>

                    <Image ImageFailed="wishlistImage_ImageFailed_1" x:Name="wishlistImage" HorizontalAlignment="Left" Width="164" Margin="12,54,0,88" Stretch="Fill">
                        <Image.Source>
                            <BitmapImage CreateOptions="DelayCreation,IgnoreImageCache" UriSource="{Binding thumb_image}" />
                        </Image.Source>
                    </Image>

            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>

这是我为我的图像设置 itemsource 的课程

[DataContract]
public class WishListResponse : INotifyPropertyChanged
{

 [DataMember]
 public List<string> thumb_images { get; set; }

 public string thumb_image
    {
        get
        {
            if (thumb_images.Count != 0)
                return thumb_images[0];
            else
                return "";
        }
        set
        {
            thumb_images[0] = value;
            OnPropertyChanged(thumb_images[0]);
        }
    }
   public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
 }

在我的主页中,我像这样绑定项目源

 public static List<WishListResponse> wishlistList = new List<WishListResponse>();

 wishListListBox.ItemsSource = wishlistList.ToArray();

谁能帮我刷新我的图像控件。 谢谢。

【问题讨论】:

  • 你能显示列表的定义和你分配它的代码/属性吗?

标签: image windows-phone-7 windows-phone-8 windows-phone


【解决方案1】:

好的,谢谢你的更新:-)

首先回答您最初的问题.. ;-) 如果您将 List 替换为 ObservableCollection,它会在您更改列表条目时自动更新 UI。

 public ObservableCollection<string> ThumbImages { get; set; }

现在,如果您将 ThumbImages 设置为新的,即为其分配一个新对象,您将不得不再次将 DataContext 设置为新的引用,例如:

ThumbImages = new ObservableCollection<string>(wishListResponse.thumb_images);
DataContext = ThumbImages;

现在通常你将 datacontext 设置为视图的模型,在你的例子中是一个字符串列表。您可以在后面的代码中设置这样的数据上下文:

DataContext = ThumbImages;

然后你可以使用默认绑定来引用数据上下文:

 <ListBox ItemsSource="{Binding}">...</ListBox>

因此,您将不再需要在 WishListResponse 上创建特殊属性并将 ItemTemplate 再次设置为 Element,即在此上下文中再次设置默认绑定:

 <ListBox ItemsSource="{Binding}" Grid.Row="1" HorizontalAlignment="Left" Margin="0,81,0,0" Name="wishListListBox" VerticalAlignment="Top" Width="480" Visibility="Visible">

        <ListBox.ItemTemplate>
            <DataTemplate>

                    <Image ImageFailed="wishlistImage_ImageFailed_1" x:Name="wishlistImage" HorizontalAlignment="Left" Width="164" Margin="12,54,0,88" Stretch="Fill">
                        <Image.Source>
                            <BitmapImage CreateOptions="DelayCreation,IgnoreImageCache" UriSource="{Binding}" />
                        </Image.Source>
                    </Image>

            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>

HTH

【讨论】:

  • 您好,感谢您的支持,您能告诉我更多有关此解决方案的信息,我需要在哪里实现此 INotifyChange,以及如何将我的 ListBox 刷新与此事件联系起来
  • @Noorul 你能提供更多代码吗? IE。你在哪里绑定数据上下文?如果您将自定义对象(您创建的对象)绑定到它。该类必须继承 INotifyPropertyChanged。
【解决方案2】:

我在这个问题上尝试了非常简单的解决方案,它对我有用,

每次我需要刷新图像时,我都会更改图像链接, 这意味着我只是将当前时间附加到图像 url。

这对我来说很好。

谢谢。

【讨论】:

    【解决方案3】:

    我认为代码的更改不会通知给 xaml,因此您必须从 INotifyChange 继承该类,然后进行属性更改事件......就像

            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
    

    然后在设置列表的属性之后

     listname = value;
    OnPropertyChanged("listname");
    

    希望对你有所帮助....

    【讨论】:

    • 您好,感谢您的支持,能否请您告诉我更多有关此解决方案的信息,我需要在哪里实现此 INotifyChange,以及如何将我的 ListBox refresh 与此事件相关联。
    • 只需使用 INotifyPropertyChange 继承您的类并引发事件,如答案所示。然后在您的列表中进行更改,它们将反映在屏幕上...
    • 我认为您必须显示一些代码,以便我们能够为您提供适当的帮助
    • 做一件事,而不是继承WishListResponse类与onproperty更改继承主页并调用onproperchanged方法,就像这个私有静态List _wishlistList;公共静态列表 愿望清单 { 获取 { 返回 _wishlistList ; } 设置 { _wishlistList = 值; OnPropertyChanged("愿望清单"); } } 然后wishlistList = new List(); wishListBox.ItemsSource = wishlistList;
    【解决方案4】:

    我认为原因是你的模型没有实现 INotifyPropertyChanged ,所以你的属性发生了变化,但无法通知图像绑定属性,解决这个问题

    一个是实现 INotifyPropertyChanged ;

    另一个你可以这样做

    wishListListBox.ItemsSource =null;
    wishListListBox.ItemsSource = itemsSource;
    

    它还可以让您的列表框显示新项目

    但是当itemsSource改变时它有问题你必须这样做............

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      相关资源
      最近更新 更多