[英]Image control not refreshed in windows phone app
我的應用程序中有一個圖像列表,它們都在列表框中,我將源代碼作為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();
任何人都可以幫助我刷新我的圖像控制。 謝謝。
好的,謝謝你的更新:-)
首先是您的原始問題.. ;-)如果您將List
替換為ObservableCollection
,它將在您更改列表條目時自動更新UI。
public ObservableCollection<string> ThumbImages { get; set; }
現在,如果您設置了ThumbImages new,即為其分配新對象,則必須再次將DataContext設置為新引用,例如:
ThumbImages = new ObservableCollection<string>(wishListResponse.thumb_images);
DataContext = ThumbImages;
現在通常將datacontext設置為視圖的模型,在您的情況下是一個字符串列表。 您可以在后面的代碼中設置如下的datacontext:
DataContext = ThumbImages;
然后,您可以使用默認綁定引用datacontext:
<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
我在這個問題上嘗試了非常簡單的解決方案,它對我有用,
我只是在每次需要刷新圖像時更改圖像鏈接,這意味着我只需將圖像網址添加到當前時間。
這對我來說很好。
謝謝。
我認為代碼中的更改不會通知給xaml,因此您必須從INotifyChange繼承該類,然后進行屬性更改事件...
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
然后在列表的屬性集之后
listname = value;
OnPropertyChanged("listname");
我希望這可以幫助你......
我覺得原因是你的模型沒有實現INotifyPropertyChanged,所以你的屬性已經改變了,但是無法通知圖片綁定屬性,要解決這個問題
一個是實現INotifyPropertyChanged;
另一個你可以這樣做
wishListListBox.ItemsSource =null;
wishListListBox.ItemsSource = itemsSource;
它還會使您的列表框顯示新項目
但是當itemsSource改變時你有問題,你必須這樣做........
希望這可以幫到你
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.