Hello, I am having a problem with reloading data on a page when the OnAppearing fires.
What appears to happen when stepping through the debugger is as follows.
The page is created, and the Task is assigned. While assigning the function to the task it is run for the first time (probably should add logic to prevent a full run when only assigning, but I can do that later)
The page is displayed with the proper record data as expected.
The user clicks a button to add a new record.
The add record page loads as expected. The add/modify data page uses the same viewmodel but creates it's own new instance (another reason to prevent the laoding of the inventory when only working on a specific record)
When the user is done and clicks save, the record is saved to the database, and the the page is closed with
await Navigation.PopAsync(true);When the inventory page is shown again, the
OnAppearingfunction fires calling theRefreshPagefunction, but when it comes to the line to reload the inventory, theInitInventoryfunction never fires.
I've tried stepping the code, but when it gets to the InitInventory line, it simply steps over it, and the function is never run. (I've tried adding debug code to output to the console when the function runs, and that output is never sent. I've also tried setting break points within the function and they never break) The watch results for the task say that the task ran to completion, but that's even before it's called (I am assuming it's a st ale state from the initialization).
How can I force the function to fire? should I just give up on the dynamic task assignment, and just call the targeted function directly?
Here is a basic rundown of the code.
BaseViewModel.cs
public class BaseViewmodel
{
....
public Task InitInventory{ get; internal set; }
....
}
itemviewmodel.cs
public class ItemViewModel : BaseViewmodel
{
....
public ItemViewModel()
{
InitInventory = ExecuteLoadItemsCommand();
}
internal async Task ExecuteLoadItemsCommand()
{
try
{
if (IsBusy == true)
return;
else
IsBusy = true;
if (ItemsList == null)
ItemsList = new ObservableCollection<ItemModel>();
ItemsList.Clear();
ItemsList = await ItemsInventoryRepository.GetItems();
foreach (var item in ItemsList)
{
item.Photos = new ObservableCollection<Photos>(await PhotoRepo.GetItemByParentID(item.Id, (int)RecordCatagory.Supplies, (int)RecordType.Product).ConfigureAwait(true));
}
//Refresh the items list for the main screen
OnPropertyChanged(nameof(ItemsList));
}
catch (Exception ex)
{
DebugTools.LogException(ex);
}
finally
{
IsBusy = false;
}
}
}
ProductInventory.xaml.cs
{
...
internal ItemViewModel viewModel;
public ProductInventory()
{
try
{
InitializeComponent();
BindingContext = (viewModel = new ItemViewModel());
BuildForm();
}
catch (Exception ex)
{
DebugTools.LogException(ex);
}
}
//When the page appears, reload the data and show it
protected override async void OnAppearing()
{
try
{
base.OnAppearing();
await RefreshPage().ConfigureAwait(true);
}
catch (Exception ex)
{
DebugTools.LogException(ex);
}
}
private async void BuildForm()
{
try
{
await viewModel.InitInventory.ConfigureAwait(true);
dataGrid.ItemsSource = viewModel.WaddingInventoryList;
//Build the display for the grid
}
catch (Exception ex)
{
DebugTools.LogException(ex);
}
}
private async Task RefreshPage()
{
try
{
if (viewModel != null)
{
await viewModel.InitInventory;
dgWadding.IsVisible = viewModel.WaddingInventoryList.Count > 0;
dgWadding.ItemsSource = viewModel.WaddingInventoryList;
dgWadding.Refresh();
}
}
catch (Exception ex)
{
DebugTools.LogException(ex);
}
}
}
As always any help is greatly appreciated!
Cheers!