UWP视图模型与多视图问题:同一条目显示异常
Let's work through this issue systematically—since you noticed the problem popped up after several recent changes, we can narrow down potential causes and verify your ViewModel reuse suspicion first.
1. Verify if ViewModel Reuse is Causing Stale Group Data
Looking at your OnAppearing code, you only initialize the HomePageVM if BindingContext is null. If your page is being cached (common in Xamarin.Forms NavigationPages), the ViewModel won't reinitialize on subsequent visits. Worse, your group refresh logic only triggers when CurrentCount doesn't match Main.API.DebtsDB.Entries.Count—this misses cases where an entry's properties change (like being pinned or marked for attention) but the total entry count stays the same.
Quick Test to Confirm:
Temporarily modify your OnAppearing method to force a refresh of groups every time the page loads, regardless of CurrentCount or BindingContext:
protected async override void OnAppearing() { base.OnAppearing(); if(BindingContext == null) { HVM = new HomePageVM(); BindingContext = HVM; } // Force refresh groups every time the page appears RefreshHomeGroups(); } // Extract group generation into a separate method for reusability private void RefreshHomeGroups() { var l = Main.API.DebtsDB.Entries.ToList(); l.Sort((x, y) => (y.Modified.CompareTo(x.Modified) * 1024) + (x.IsFather.CompareTo(y.IsFather) * -1)); // ... rest of your group creation code here ... // Update the HVM's groups collection (make sure it's an ObservableCollection!) HVM.Groups = new ObservableCollection<HomeDataGroup>(ls); }
If the entry now shows up in the correct groups, your ViewModel reuse + count-based refresh logic was the root issue.
2. Check if Database Changes Broke Filter Logic
Switching from Lex.db to LiteDB could alter how LINQ queries are executed, or how your entry properties are persisted. Let's validate the data itself:
Debug Key Properties for "Achat de Voiture":
Set a breakpoint right before you create the groups, and check these values for the problematic entry:
IsNormalDebt: Must betrue(all your group filters require this)PinnedUporPinnedUpPlus: Must betrueto show in "Pinned"EmotionWarning: Must betrueto show in "Need Attention"ModifiedDate: Should be within the last 14 days (which it is, since it shows in "Recents")
Verify LiteDB Data Directly:
Use a LiteDB viewer tool to open your local database file and confirm the PinnedUp, EmotionWarning, and IsNormalDebt fields for "Achat de Voiture" are set correctly. It's possible the data migration from Lex.db to LiteDB missed these fields, or Firebase sync isn't updating them properly after the library switch.
3. Rule Out Xamarin.Forms Update Issues
Updating Xamarin.Forms can introduce subtle binding or ListView grouping behavior changes:
- Check
HomeDataGroupImplementation: Ensure it inherits fromObservableCollection<T>and implementsINotifyPropertyChangedfor properties likeCountandTitle. If the group'sCountisn't updating correctly, the ListView might hide the group even if it has entries. - Validate DataTemplate Binding: Double-check that your ListView's
IsGroupingEnabledis set totrue, and that you're binding to the groups collection correctly in your XAML (e.g.,ItemsSource="{Binding Groups}").
4. Firebase Library Switch Check
If your app syncs data from Firebase to the local LiteDB, confirm that the new Firebase library is correctly deserializing all properties (like PinnedUp and EmotionWarning) when pulling data. A missing property mapping could result in default false values, which would exclude the entry from non-Recents groups.
Next Steps
- First run the ViewModel refresh test to eliminate reuse as a cause.
- If that doesn't fix it, debug the entry's properties and check the LiteDB data directly.
- Verify that your LINQ filters behave the same in LiteDB as they did in Lex.db (some LINQ operators might have different implementations).
内容的提问来源于stack exchange,提问作者Fabrice T

