question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NavigationView throws an exception when change the SelectedItem in code

See original GitHub issue

Describe the bug Hi, I tried to change the SelectedItem of the NavigationView Control in code but i get an exception, I tried two different codes:

myNavigatoinView.SelectedItem = oneNavigationViewItem;

and

oneNavigationViewItem.IsSelected = true;

Both codes return the following exception:

System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object. Source=FluentAvalonia StackTrace: at FluentAvalonia.UI.Controls.SelectionModel.<>c.<get_SelectedItems>b__39_1(List1 infos, Int32 index) at FluentAvalonia.UI.Controls.SelectedItems2.get_Item(Int32 index) at FluentAvalonia.UI.Controls.SelectionModel.get_SelectedItem() at FluentAvalonia.UI.Controls.NavigationView.OnSelectionModelSelectionChanged(Object sender, SelectionModelSelectionChangedEventArgs e) at FluentAvalonia.UI.Controls.SelectionModel.OnSelectionChanged(SelectionModelSelectionChangedEventArgs e) at FluentAvalonia.UI.Controls.SelectionModel.EndOperation() at FluentAvalonia.UI.Controls.SelectionModel.Operation.Dispose() at FluentAvalonia.UI.Controls.SelectionModel.SelectAt(IndexPath index) at FluentAvalonia.UI.Controls.NavigationView.UpdateSelectionModelSelection(IndexPath ip) at FluentAvalonia.UI.Controls.NavigationView.OnNavigationViewItemIsSelectedPropertyChanged(AvaloniaPropertyChangedEventArgs args) at Avalonia.Reactive.LightweightObservableBase1.PublishNext(T value) at Avalonia.AvaloniaObject.RaisePropertyChanged[T](AvaloniaPropertyChangedEventArgs1 change) at Avalonia.AvaloniaObject.Avalonia.PropertyStore.IValueSink.ValueChanged[T](AvaloniaPropertyChangedEventArgs1 change) at Avalonia.ValueStore.NotifyValueChanged[T](AvaloniaProperty1 property, Optional1 oldValue, BindingValue1 newValue, BindingPriority priority) at Avalonia.ValueStore.SetValue[T](StyledPropertyBase1 property, T value, BindingPriority priority) at Avalonia.AvaloniaObject.SetValue[T](StyledPropertyBase1 property, T value, BindingPriority priority) at Avalonia.Controls.ListBoxItem.set_IsSelected(Boolean value) at Nimloth.Views.MainView.ChangeNavContent(NavigationViewItem nvi) in C:\Users\guilh\Documents\Acadêmico\VisualStudio\Nimloth\Nimloth Avalonia\Views\MainView.axaml.cs:line 191

Desktop/Platform (please complete the following information):

  • OS: Windows 10
  • FluentAvalonia Version [e.g. 1.3.4]
  • Avalonia Version [e.g. 0.10.14]

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
amwxcommented, Jun 5, 2022

I think this issue is related to ReactiveUI. Replacing the ReactiveCommand.Create with a basic ICommand implementation and the null ref issue went away. This would also align with why the click event works without issue

I don’t like nor use RxUI so I don’t know much about it, but IIRC it has it’s own task scheduler which might be causing some weird behavior leading to the null ref.

1reaction
amwxcommented, May 29, 2022

How did you set up the NavigationView? Binding or hard-coded items? A minimal repro would be great or at least a little more context so I can try to reproduce the error

Read more comments on GitHub >

github_iconTop Results From Across the Web

NavigationView - When selected settings item there is no ...
Works like a charm when items are set explicitly but throws Critical Exception if ItemsSource is set to some list unfortunately. – Artur...
Read more >
Clicking on NavigationView's Settings item throws ...
When starting a new app, it's easy to get into a state where the app throws a NullReferenceException. The way an app developer...
Read more >
NavigationView - Windows apps
SelectionChanged can be raised by a user invoking an item that isn't currently selected, or by programmatically changing the selected item. If ...
Read more >
Configuring NavigationService and NavigationView in one ...
The task of one place to configure NavigationService and NavigationView really creates a convenience for making changes.
Read more >
SOLVED: Saving list item selection on main view change
I'd like to keep selected item on StudentsView when I change to Works on MainView, and back again (and viceversa). Now doing so...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found