Closed robloo closed 3 months ago
Do you have a repro, or if not an actual project at least a description of how the NavView is set up so I can repro this?
I was afraid you were going to say that. Reducing down to a repro in a full app is always tricky. Might be faster to debug with a local build of FluentAvalonia itself. Will spend more time on this later in the week.
If you can't get a repro, does the error message have the line number - I didn't see it above.
Also, populating the NavView, are you binding or using raw NavigationViewItems? If binding, what does your data template look like and what is the source collection type? Is SelectedItem bound? Is it hierarchical? PaneDisplayMode? Basically anything you can tell me about how its set up and I can try to repro it myself. Does it crash upon load or do you have to do anything?
All I know is just from a quick look at the code and stepping back from how that final method is called, nothing should be null at that point. The only thing I can think of is maybe GetElement
is being called too early from a binding (like SelectedItem being set) and layout hasn't run so the ScrollViewer isn't available yet, but that should repro in more cases if that's the case.
Edit: Somehow it submitted while I was typing... so I finished it.
@amwx I'm running with FluentAvalonia source directly now so can step into the code.
Also, populating the NavView, are you binding or using raw NavigationViewItems?
NavigationViewItem are added directly in XAML -- there is no binding here.
Is SelectedItem bound?
No
Is it hierarchical?
No
PaneDisplayMode?
Top
Does it crash upon load or do you have to do anything?
It crashes on initial load. It's crashing as soon as Show() is called on the Window and the first layout/arrange pass is running. This is before the Window is even loaded and before and more complex code is run. As far as I can tell it's still constructing the visual tree and not even running real app code yet.
ScrollViewer isn't available yet
That is indeed what is happening. I notice most usages of _scroller have a null check as well. The area of the crash there is no check.
@amwx The below does work around the issue. App starts up and runs normally after this change adding the null checks. Somehow the code path is getting run before EnsureScroller() or something. Will look further.
public void OnElementPrepared(Control element, VirtualizationInfo vInfo)
{
// If we have an anchor element, we do not want the
// scroll anchor provider to start anchoring some other element.
vInfo.CanBeScrollAnchor = true;
- _scroller.RegisterAnchorCandidate(element);
+ _scroller?.RegisterAnchorCandidate(element);
}
public void OnElementCleared(Control element)
{
// Unlike OnElementPrepared, we can't make the change and add the virt info
// as the caller of this (ItemsRepeater.ClearElementImpl) doesn't have a ref
// and that method has multiple refs which don't have a the virt info either
ItemsRepeater.GetVirtualizationInfo(element).CanBeScrollAnchor = false;
- _scroller.UnregisterAnchorCandidate(element);
+ _scroller?.UnregisterAnchorCandidate(element);
}
Ok I've added in the null checks and stopped this crash. Everything is actually running ok, but in Top pane mode there isn't a ScrollViewer in the tree and so it was never set. This code is slightly different from WinUI because Avalonia handles scroll anchor candidates slightly differently. So adding the null check won't hurt anything since if there's no ScrollViewer, there's no need to register anchor candidates.
2.1 branch has been updated with this fix along with a couple other crashes I found. If you find any other issues, just tack them on here.
Describe the bug
The preview
NavigationView
crashes in theItemsRepeater
ItemsRepeater.OnElementPrepared / ViewportManager.OnElementPrepared code. This was previously working in preview2 and earlier versions of FluentAvalonia 2.0+.Screenshots
Desktop/Platform (please complete the following information):
Additional context