0
我正在使用MVVM模式製作Windows商店應用程序。當我從JSON文件加載字典並將其綁定到列表視圖時,每當我切換視圖時它都不會更新。有時候,listview正在更新,有時候不會。有什麼出錯的建議?Listview將不會每次更新時,我正在加載JSON文件
類KundePersistency
public static async Task<List<Kunde>> LoadKunderFromJsonAsync()
{
string kundeJsonString = await DeserializekunderFileAsync(JsonFileKunder);
if (kundeJsonString != null)
return (List<Kunde>)JsonConvert.DeserializeObject(kundeJsonString, typeof(List<Kunde>));
return null;
}
private static async Task<string> DeserializekunderFileAsync(string fileName)
{
try
{
StorageFile localFile = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
return await FileIO.ReadTextAsync(localFile);
}
catch (FileNotFoundException)
{
return null;
}
}
類BookingSystem(視圖模型)
private async void LoadKunder()
{
_id = 1;
var loadedKunder = await KundePersistency.LoadKunderFromJsonAsync();
if (loadedKunder != null)
{
KundeRegister.KundeMedId.Clear();
foreach (var kunde in loadedKunder)
{
KundeRegister.KundeMedId.Add(_id++, kunde);
}
}
}
類KundeRegister(型號)
public class KundeRegister : INotifyPropertyChanged
{
public Dictionary<int, Kunde> KundeMedId { get; set; }
public KundeRegister()
{
KundeMedId = new Dictionary<int, Kunde>();
KundeMedId.Add(Kunde.Id,new Kunde("bob","bob","bob","bobbobbo","bob","bob"));
KundeMedId.Add(Kunde.Id, new Kunde("bob", "bob", "bob", "bobbobbo", "bob", "bob"));
}
public void AddKunde(string username, string password,string adresse,string email, string name, string tlf)
{
KundeMedId.Add(Kunde.Id, new Kunde(adresse, email, name, tlf, username, password));
OnPropertyChanged();
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
這是行得通的。非常感謝你! – Flaugzig
很高興它確實工作 – ZeeMoussa