I have a XAML form linked to an MVVM with the binding. At the first call the form is rarely populated, from the second onwards, it works sporadically. No errors are ever generated.
This is the code:
public class IspezioneFile
{
public string NomeFile { get; set; } = string.Empty;
public int NumeroIspezioni { get; set; } = 0;
public int NumeroAnomalie { get; set; } = 0;
public int NumeroFoto { get; set; } = 0;
public bool ErrorFile { get; set; } = false;
public bool CanDelete { get; set; } = false;
ICommand UploadCloud;
public ICommand UploadCloudCommand =>
UploadCloud ??
(UploadCloud = new Command(async () => await ExecuteUploadCloudCommand()));
private async Task ExecuteUploadCloudCommand()
{
Application.Current?.MainPage?.DisplayAlert("Upload", "zzz", "Ok", "no");
}
}
public class IspezioneFileRoot
{
public List<IspezioneFile> Items { get; set; }
}
public class IspezioneFilesVM : BaseViewModel
{
IspezioneFileRoot listFilesIsp;
public IspezioneFileRoot ListFilesIsp
{
get { return listFilesIsp; }
set { listFilesIsp = value; OnPropertyChanged(); }
}
ICommand getFiles;
public ICommand GetFilesCommand =>
getFiles ??
(getFiles = new Command(async () => await ExecuteGetFilesCommand()));
private async Task ExecuteGetFilesCommand()
{
if (IsBusy)
return;
IsBusy = true;
try
{
VariabiliGlobali VarGlobal = VariabiliGlobali.Instance();
//var list = System.IO.Directory.GetFiles(VarGlobal.pathDocuments, "*.json");
//if (list.Length > 0)
//{
// for (int i = 0; i < list.Length; i++)
// {
// System.IO.File.Delete(list[i]);
// }
//}
PCLStorage.IFolder folder = await PCLStorage.FileSystem.Current.GetFolderFromPathAsync(VarGlobal.pathDocuments);
var ListFiles = await folder.GetFilesAsync();//istruzione per trovare tutti i dati
ListFilesIsp = new BmsClassi.IspezioneFileRoot();
ListFilesIsp.Items=new List<BmsClassi.IspezioneFile>();
foreach (var item in ListFiles)
{
BmsClassi.IspezioneFile InfoFile = new BmsClassi.IspezioneFile();
await ExecuteGetFilesTask(InfoFile, item, folder);
if (VarGlobal.TipoUser=="Amministratore") { InfoFile.CanDelete = true; };
ListFilesIsp.Items.Add(InfoFile);
}
}
catch (Exception ex)
{
//Temp = "Unable to get Weather";
System.Diagnostics.Debug.WriteLine(ex.Message);
IsBusy = false;
}
finally
{
IsBusy = false;
}
}
private async Task<BmsClassi.IspezioneFile> ExecuteGetFilesTask(BmsClassi.IspezioneFile InfoFile, PCLStorage.IFile file, PCLStorage.IFolder folder)
{
var jsonWork = await UtilsPCLstorage.ReadAllTextAsync(file.Name, folder);
InfoFile.NomeFile = file.Name;
try
{
List<BmsClassi.BmsIspezioni> tempListIsp = JsonConvert.DeserializeObject<List<BmsClassi.BmsIspezioni>>(jsonWork);
InfoFile.NumeroIspezioni = tempListIsp.Count();
if (InfoFile.NumeroIspezioni > 0)
{
InfoFile.NumeroAnomalie = (from p in tempListIsp select p.AnomalieRilevateVM.AnomalieItemsList.Count).Sum();//http://www.csharp-examples.net/linq-sum/
InfoFile.NumeroFoto = (from p in tempListIsp select p.SegnalazioneVM.ListPhoto.Count).Sum();
}
}
catch (Exception ex)
{
InfoFile.ErrorFile = true;
}
return InfoFile;
}
}
<ContentPage.Content>
<StackLayout>
<Grid BackgroundColor="LightGray" Padding="5">
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.26*"/>
<ColumnDefinition Width="0.18*"/>
<ColumnDefinition Width="0.18*"/>
<ColumnDefinition Width="0.18*"/>
<ColumnDefinition Width="0.10*"/>
<ColumnDefinition Width="0.10*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Text="Nome File" Style="{StaticResource LabelHeader}" HorizontalOptions="Start" />
<Label Grid.Column="1" Text="N° Ispezioni" Style="{StaticResource LabelHeader}" TextColor="Red"/>
<Label Grid.Column="2" Text="N° Anomalie" Style="{StaticResource LabelHeader}" TextColor="Blue"/>
<Label Grid.Column="3" Text="N° Foto" Style="{StaticResource LabelHeader}" TextColor="Black"/>
</Grid>
<ListView ItemsSource="{Binding ListFilesIsp.Items}"
HasUnevenRows="True"
CachingStrategy="RecycleElement"
IsPullToRefreshEnabled="True"
RefreshCommand="{Binding GetFilesCommand}"
IsRefreshing="{Binding IsBusy, Mode=OneWay}"
RowHeight="75"
x:Name="ListViewJsonFiles">
<ListView.SeparatorColor>
<OnPlatform x:TypeArguments="Color" iOS="Transparent"/>
</ListView.SeparatorColor>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.26*"/>
<ColumnDefinition Width="0.18*"/>
<ColumnDefinition Width="0.18*"/>
<ColumnDefinition Width="0.18*"/>
<ColumnDefinition Width="0.10*"/>
<ColumnDefinition Width="0.10*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Text="{Binding NomeFile}" Style="{StaticResource LabelHeader}" FontSize="15" HorizontalOptions="Start" />
<Label Grid.Column="1" Grid.ColumnSpan="3" Text="Errore lettura file" IsVisible="{Binding ErrorFile}" Style="{StaticResource LabelContatore}"/>
<Label Grid.Column="1" Text="{Binding NumeroIspezioni}" Style="{StaticResource LabelContatore}" TextColor="Red"/>
<Label Grid.Column="2" Text="{Binding NumeroAnomalie}" Style="{StaticResource LabelContatore}" TextColor="Blue"/>
<Label Grid.Column="3" Text="{Binding NumeroFoto}" Style="{StaticResource LabelContatore}" TextColor="Black"/>
<Image Grid.Column="4" Source="UploadToCloud.png" >
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding UploadCloudCommand}" NumberOfTapsRequired="2"/>
</Image.GestureRecognizers>
</Image>
<Image Grid.Column="5" Source="trash.png" IsVisible="{Binding CanDelete}"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
public DataForm ()
{
InitializeComponent ();
this.BindingContext = IspFilesVM;
IspFilesVM.GetFilesCommand.Execute(null);
}
What's the problem?!?