NavigationView throws an exception when change the SelectedItem in code
See original GitHub issueDescribe 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.SelectedItems
2.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](AvaloniaPropertyChangedEventArgs
1 change)
at Avalonia.AvaloniaObject.Avalonia.PropertyStore.IValueSink.ValueChanged[T](AvaloniaPropertyChangedEventArgs1 change) at Avalonia.ValueStore.NotifyValueChanged[T](AvaloniaProperty
1 property, Optional1 oldValue, BindingValue
1 newValue, BindingPriority priority)
at Avalonia.ValueStore.SetValue[T](StyledPropertyBase1 property, T value, BindingPriority priority) at Avalonia.AvaloniaObject.SetValue[T](StyledPropertyBase
1 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:
- Created a year ago
- Comments:5 (2 by maintainers)
I think this issue is related to ReactiveUI. Replacing the
ReactiveCommand.Create
with a basicICommand
implementation and the null ref issue went away. This would also align with why the click event works without issueI 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.
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