So, I'm developing a contact management system and I'm trying to add a contact. Instead of jumping between ViewModels(the contact and main contact page are separate), I've decided to take in data from the view, create a contact, add it to a list then serialize that list. Then, when I get back to the main page, I deserialize that list.
This works fine(no optimization, this is just a simple college project) as the data is added to the JSON file. My problem is- when I navigate back to the main page- the list doesn't update due to an UnauthorisedAccessException on my stream. The deserialization method is:
private async void buildMyListWithJsonAsync(){
ObservableCollection<Contact> list = new ObservableCollection<Contact>();
try
{
string JSONFILENAME = "contacts.json";
string content = " ";
StorageFile File = await ApplicationData.Current.LocalFolder.GetFileAsync(JSONFILENAME);
using (IRandomAccessStream testStream = await File.OpenAsync(FileAccessMode.Read)){
using (DataReader dreader = new DataReader(testStream)){
uint length = (uint)testStream.Size;
await dreader.LoadAsync(length);
content = dreader.ReadString(length);
list = JsonConvert.DeserializeObject<ObservableCollection<Contact>>(content);
}
}
contactlist = new ObservableCollection<Contact>();
foreach (Contact c in list)
contactlist.Add(c);
}
catch (Exception e)
{ e.ToString(); }
}
Any help or aid would be appreciated.