Reputation: 51
In my main ViewModel, I am trying to go to another view and pass an object through the Dictionary and use it to load values in that view. My issue is that it works the first time but is stuck on an infinite load afterwards due to the object being passed is null each time after the first.
For example, if I click on the button to call OpenSpecialInstructionsCommand, it'll navigate me to the SpecialInstructions view and ViewModel, show me everything I need to see. When I am done, I click the back arrow to go to the main page and find another object that I would like to check the special instructions on. If I click the button to call OpenSpecialInstructionsCommand now, it navigates me to SpecialInstructions but it's stuck on an infinite load. I can go back and try again on the same/different item or even the original and it will still load indefinitely.
Main ViewModel:
[RelayCommand]
async Task OpenSpecialInstructions()
{
if (LineItem == null)
return;
this.popupPage.Close();
await Shell.Current.GoToAsync(nameof(SpecialInstructions), true, new Dictionary<string,
object>
{
{"LineItem", LineItem }
});
}
This is working fine; I have the correct values of my LineItem object each time this is called.
In the new ViewModel:
[QueryProperty(nameof(LineItem), "LineItem")]
public partial class SpecialInstructionsViewModel : BaseViewModel
{
[ObservableProperty]
LineItem lineItem;
[RelayCommand]
async Task Instructions_Get()
{
IsBusy = true;
if (App.SpecialInstructions.Count() == 0)
{
await PrefillSpecialInstructions();
}
SpecialInstructions = App.SpecialInstructions;
foreach (SpecialInstruction instruction in App.DbConnection.SpecialInstructions_Get(LineItem.StockNumber))
{
try { SpecialInstructions.FirstOrDefault(x => x.MessageCode == instruction.MessageCode).IsSelected = true; } catch { }
}
IsBusy = false;
}
}
So the weird part to me is that LineItem is null until we get to the line where SpecialInstructions = App.SpecialInstructions;
is called, I thought it would have been initialized sooner than that. Besides that, when I try to run through the foreach loop, it never gets into it because it's getting caught up on the LineItem.StockNumber
part for the getter in my local database.
I guess my question is why is LineItem null each time after the first time?
Upvotes: 1
Views: 272
Reputation: 51
So for some reason, I added
if (LineItem == null)
{
IsBusy = false;
return;
}
right before the foreach loop and now it works fine. I can't understand why that's the case but it is working every time now.
Upvotes: 0