I am using a loop to place images in my canvas, there is a method that needs to check if each looped images have an attribute that is stored in a JSON file.
the checkImage method works fine outside the loop, but when I place it in the loop it gives this error:
"Access denied, file is used by another process"
Since the method gets called for every image created by the loop I guess that I need to close the JSON file after its serialized or something, but how?
The loop:
while (i < blauw)
{
checkImage(i); //checks if it has the attribute or not
ImageBrush brush = new ImageBrush();
Button Btn = new Button();
Uri uri = new Uri("ms-appx://Blijfie/images/" + selected + "/" + blauwpics[i]);
BitmapImage imgSource = new BitmapImage(uri);
brush.ImageSource = imgSource;
Btn.Background = brush;
Btn.Width = 200;
Btn.Height = 200;
if (i >= 5 && i < 10)
{
Canvas.SetTop(Btn, marginTop);
Canvas.SetLeft(Btn, marginLeft2);
marginLeft2 = marginLeft2 + 250;
}
else if (i < 6)
{
Canvas.SetLeft(Btn, marginLeft);
marginLeft = marginLeft + 250;
}
else if (i >= 10)
{
Canvas.SetTop(Btn, marginTop2);
Canvas.SetLeft(Btn, marginLeft3);
marginLeft3 = marginLeft3 + 250;
}
main.Children.Add(Btn);
i++;
}
CheckImage method:
private async void checkImage(int id) {
var foldertest = ApplicationData.Current.TemporaryFolder;
var files = await foldertest.GetFilesAsync();
var desiredFile = files.FirstOrDefault(x => x.Name == "Dierendb.json");
var textContent = await FileIO.ReadTextAsync(desiredFile);
var result = JsonConvert.DeserializeObject<List<Dier>>(textContent);
id++;
if (result[id].PathDier.Equals(""))
{
//do this
}
else
{
//do that
}
string dierenlijst = JsonConvert.SerializeObject(result, Formatting.Indented);
await Windows.Storage.FileIO.WriteTextAsync(desiredFile, dierenlijst, Windows.Storage.Streams.UnicodeEncoding.Utf8);
}