在Windows商店应用程序上工作时,我尝试在更新某些数据时更新/刷新listView。但是,尽管我阅读了所有样本和文档,但它不起作用......
这里我的代码背后: (我不提供我的xaml文件,它只是一个示例listView。)
所以实现INotifyPropertyChanged接口的类:
public class Download : INotifyPropertyChanged
{
public enum DownloadState
{
Running,
Waiting,
Pausing,
Paused,
Cancelling,
Cancelled
};
private String Uri;
private StorageFile storageFile;
private String tempFileName;
private String fileName;
private String version ;
private long totalSize ;
private long downloadedBytes;
private DownloadState state;
private Protocol protocol;
public Download(String Uri, StorageFile file, String fileName, String version, long totalSize, Protocol protocol)
{
this.Uri = Uri;
this.storageFile = file;
this.tempFileName = "";
this.fileName = fileName;
this.version = version;
this.totalSize = totalSize;
this.downloadedBytes = 0;
this.state = DownloadState.Waiting;
this.protocol = protocol;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
System.Diagnostics.Debug.WriteLine("Update!"); //ok
if (PropertyChanged != null)
{
//PropertyChanged is always null and shouldn't.
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public DownloadState State
{
get{return this.state;}
set {
this.state = value;
NotifyPropertyChanged();
}
}
//+some others methods
}
}
地铁应用程序的主页:
// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237
namespace ClientAirNavLight_WS
{
/// <summary>
/// A basic page that provides characteristics common to most applications.
/// </summary>
public sealed partial class MainPage : ClientAirNavLight_WS.Common.LayoutAwarePage
{
/// <summary>
/// Represent a Web Service proxy.
/// </summary>
private AirNavLight_WSClientClient proxyWS;
/// <summary>
/// Initiialize the component of the application's main page.
/// </summary>
public MainPage()
{
this.InitializeComponent();
}
/// <summary>
/// Populates the page with content passed during navigation. Any saved state is also
/// provided when recreating a page from a prior session.
/// </summary>
/// <param name="navigationParameter">The parameter value passed to
/// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
/// </param>
/// <param name="pageState">A dictionary of state preserved by this page during an earlier
/// session. This will be null the first time a page is visited.</param>
protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
}
/// <summary>
/// Preserves state associated with this page in case the application is suspended or the
/// page is discarded from the navigation cache. Values must conform to the serialization
/// requirements of <see cref="SuspensionManager.SessionState"/>.
/// </summary>
/// <param name="pageState">An empty dictionary to be populated with serializable state.</param>
protected override void SaveState(Dictionary<String, Object> pageState)
{
}
//Simulate data update.
private async void Button_Click(object sender, RoutedEventArgs e)
{
object selected = this.ListResult.SelectedItem;
if (selected != null)
{
//simulate an update in data.
// ListView should be refresh in order to reflect changes made here.
Download dl = (Download)selected;
if (dl.State == Download.DownloadState.Paused)
{
dl.State = Download.DownloadState.Running;
}
else
{
dl.State = Download.DownloadState.Paused;
}
}
else
{
//Just add an item to the list view.
StorageFile file = await this.getStorageFile("test");
Download dl = new Download("192.128.2.14", file, "test", "1.2", 100, Protocol.HTTP);
this.ListResult.Items.Add(dl);
this.ListResult.DataContext = dl;// Does not work.
}
}
private async Task<StorageFile> getStorageFile(String suggestedFileName)
{
FileSavePicker savePicker = new FileSavePicker();
savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
// Dropdown of file types the user can save the file as
savePicker.FileTypeChoices.Add("Application/pdf", new List<string>() { ".pdf" });
savePicker.FileTypeChoices.Add("Archive", new List<string>() { ".zip", ".rar", ".7z" });
savePicker.FileTypeChoices.Add("Plain-text", new List<string>() { ".txt" });
// Default file name if the user does not type one in or select a file to replace
savePicker.SuggestedFileName = suggestedFileName;
return await savePicker.PickSaveFileAsync();
}
}
}
那么我应该如何使用listView.DataContext?我误解了如何使用INotifyPropertyChanged?
编辑:
我的listView是如何定义的:
<ListView x:Name="ListResult" HorizontalAlignment="Left" Height="200" Margin="10,56,0,0" VerticalAlignment="Top" Width="653" SelectionMode="Single"/>
答案 0 :(得分:3)
在您的下载类中,将在UI中更新的唯一属性是State,因为它具有getter和setter。
如果没有你的XAML,就无法看到你想要显示的内容,但通常情况下,你会将DataContext对象的属性(在本例中为dl)绑定到listview可以显示的datatemplate中的不同控件。
但是这些属性需要是公共的,并且有INotifyPropertyChanged的getter和setter来更新这些属性绑定的UI。
例如,如果您希望文件名显示在ListView中,您至少需要声明filename属性,如下所示:
public String fileName {get; set; }
答案 1 :(得分:3)
你为什么要设置this.ListResult.DataContext = dl
?处理ListView时,ItemsSource是所有迭代的对象的集合,而不是DataContext。此外,ItemsSource应该是一个集合而不是一个项目。因此,如果您绑定到某个属性,该属性应该作为ItemsSource集合中的项属性存在。
但是,您似乎没有集合,因此您将dl直接添加到带有this.ListResult.Items.Add(dl)
的ListView。该方法应该有效,但是取出this.ListResult.DataContext = dl
或将this.ListResult.ItemsSource
设置为集合并将dl添加到该集合
我不熟悉[CallerMemberName]
但如果您遇到propertyName为null的问题,请尝试以下
public DownloadState State
{
get{return this.state;}
set {
if( this.state != value )
{
this.state = value;
NotifyPropertyChanged("State");
}
}
}
此外,您要绑定到的所有属性都应该是public并调用NotifyPropertyChanged,如下所示
private long totalSize ;
public long TotalSize
{
get{return this.totalSize;}
set {
if( this.totalSize != value )
{
this.totalSize = value;
NotifyPropertyChanged("TotalSize");
}
}
}
答案 2 :(得分:-1)
Zangdak - 解决这个问题的另一种方法是每次集合中的项目更改时,只需将ListView的ItemSource设置为null,然后将其设置回您的集合。如果您这样做,UI将更新,您将看到新项目已添加到您的收藏中。
Paige Ake