MAUI Shell导航第一次访问后字典为空

huangapple go评论64阅读模式
英文:

MAUI Shell navigation with Dictionary is null after first visit

问题

在我的主 ViewModel 中,我正在尝试转到另一个视图并通过字典传递一个对象,并在该视图中使用它来加载值。我的问题是第一次可以运行,但之后由于传递的对象每次都为 null,导致无限加载。

例如,如果我点击调用 OpenSpecialInstructionsCommand 的按钮,它会导航到 SpecialInstructions 视图和 ViewModel,并显示我需要看到的一切。完成后,我点击返回箭头返回主页,并查找另一个我想检查特殊说明的对象。如果我现在点击调用 OpenSpecialInstructionsCommand 的按钮,它会导航到 SpecialInstructions,但卡在无限加载上。我可以返回并在相同/不同的项目上再次尝试,甚至可以在原始项目上再次尝试,但它仍将无限加载。

主 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 }
   });
}

这个工作得很好;每次调用时,我都有 LineItem 对象的正确值。

在新 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;
   }
}

对我来说奇怪的部分是,在调用 SpecialInstructions = App.SpecialInstructions; 的地方之前,LineItem 为 null,我认为它会在此之前初始化。除此之外,当我尝试运行 foreach 循环时,它永远不会进入其中,因为它卡在了本地数据库中 getter 的 LineItem.StockNumber 部分。我想知道的是,为什么 LineItem 在第一次之后的每次调用时都是 null?

英文:

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&lt;string, 
   object&gt;
   {
       {&quot;LineItem&quot;, 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), &quot;LineItem&quot;)]

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 =&gt; 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?

答案1

得分: 0

因为某种原因,我在foreach循环之前添加了以下代码:

if (LineItem == null)
{
   IsBusy = false;
   return;
}

现在它正常工作了。我不明白为什么会这样,但现在它每次都能正常工作。

英文:

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.

huangapple
  • 本文由 发表于 2023年6月16日 02:02:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76484381.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定