'Observable Collection crashes while updating values
Not able to replace entire observable collection. As it throws exception. How do I replace entire observable collection item? The suggested answers from other threads didn't work. Tried the following. Any suggestion would help on this.
Xaml
<Picker
x:Name=“Picker1”
Style="{StaticResource PickerStyle}"
ItemsSource="{Binding CodeNumberList}"
SelectedItem="{Binding CodeNumberText}”>
<Picker.Behaviors>
<xct:EventToCommandBehavior
EventName="SelectedIndexChanged"
Command="{Binding ItemChangedCommand}"
CommandParameter="{Binding Source={x:Reference Picker1},
Path=SelectedItem}"/>
</Picker.Behaviors>
</Picker>
<Picker
x:Name=“Picker2”
Style="{StaticResource PickerStyle}"
ItemsSource="{Binding PhoneNumberList}"
SelectedItem="{Binding PhoneNumberText}”>
</Picker>
ViewModel
private ObservableCollection<string> _phumberList;
public ObservableCollection<string> PhNumberList
{
get
{
return _phumberList;
}
set
{
_phumberList = value;
RaisePropertyChanged(nameof(this.PhNumberList));
}
}
private void ItemChangedCommandHandler(string obj)
{
PopulatePhoneList();
}
private void PopulatePhoneList()
{
// tried clearing PhNumberList.Clear() and then added items, that didn't work too
//Invalid cast exception thrown in below line
PhNumberList = new ObservableCollection<string>();
foreach (var item in CurrentUser?.InformationDetailItems)
{
PhNumberList.Add (item.PhoneNumber);
}
}
public On NavigatedTo(){
// populate default pickerList
PopulatePhoneList();
}
Exception : //Invalid cast exception thrown in below line
PhNumberList = new ObservableCollection();
Stack Trace
at (wrapper castclass) System.Object.__castclass_with_cache(object,intptr,intptr)
at Prism.Commands.DelegateCommand`1[T].CanExecute (System.Object parameter) [0x00000] in d:\a\1\s\Source\Prism\Commands\DelegateCommand{T}.cs:113
at Prism.Commands.DelegateCommandBase.System.Windows.Input.ICommand.CanExecute (System.Object parameter) [0x00000] in d:\a\1\s\Source\Prism\Commands\DelegateCommandBase.cs:67
at Xamarin.CommunityToolkit.Behaviors.EventToCommandBehavior.OnTriggerHandled (System.Object sender, System.Object eventArgs) [0x00039] in <823f4019dc404dea9e9331c7114e4863>:0
at Xamarin.Forms.Picker.OnSelectedIndexChanged (System.Object bindable, System.Object oldValue, System.Object newValue) [0x00012] in D:\a\1\s\Xamarin.Forms.Core\Picker.cs:283
at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent) [0x00120] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:512
at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x00173] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:446
at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, System.Boolean fromStyle, System.Boolean checkAccess) [0x0004d] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:374
at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:349
at Xamarin.Forms.Picker.set_SelectedIndex (System.Int32 value) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Picker.cs:114
at Xamarin.Forms.Picker.OnItemsCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00007] in D:\a\1\s\Xamarin.Forms.Core\Picker.cs:205
at (wrapper delegate-invoke) <Module>.invoke_void_object_NotifyCollectionChangedEventArgs(object,System.Collections.Specialized.NotifyCollectionChangedEventArgs)
at System.Collections.ObjectModel.ObservableCollection`1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00018] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs:263
at System.Collections.ObjectModel.ObservableCollection`1[T].OnCollectionReset () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs:362
at System.Collections.ObjectModel.ObservableCollection`1[T].ClearItems () [0x00018] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs:166
at System.Collections.ObjectModel.Collection`1[T].Clear () [0x00014] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/Common/src/CoreLib/System/Collections/ObjectModel/Collection.cs:81
at Xamarin.Forms.Internals.LockableObservableListWrapper.InternalClear () [0x00000] in D:\a\1\s\Xamarin.Forms.Core\LockableObservableListWrapper.cs:71
at Xamarin.Forms.Picker.ResetItems () [0x00009] in D:\a\1\s\Xamarin.Forms.Core\Picker.cs:273
at Xamarin.Forms.Picker.OnItemsSourceChanged (System.Collections.IList oldValue, System.Collections.IList newValue) [0x0004c] in D:\a\1\s\Xamarin.Forms.Core\Picker.cs:231
at Xamarin.Forms.Picker.OnItemsSourceChanged (Xamarin.Forms.BindableObject bindable, System.Object oldValue, System.Object newValue) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Picker.cs:213
at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent) [0x00120] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:512
at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x00173] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:446
at Xamarin.Forms.BindingExpression.ApplyCore (System.Object sourceObject, Xamarin.Forms.BindableObject target, Xamarin.Forms.BindableProperty property, System.Boolean fromTarget) [0x00226] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:160
at Xamarin.Forms.BindingExpression.Apply (System.Boolean fromTarget) [0x0003e] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:56
at Xamarin.Forms.BindingExpression+BindingExpressionPart.<PropertyChanged>b__49_0 () [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:762
at Xamarin.Forms.BindingExpression+BindingExpressionPart.PropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs args) [0x000cb] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:773
at Xamarin.Forms.BindingExpression+WeakPropertyChangedProxy.OnPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e) [0x00012] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:666
at (wrapper delegate-invoke) <Module>.invoke_void_object_PropertyChangedEventArgs(object,System.ComponentModel.PropertyChangedEventArgs)
at Prism.Mvvm.BindableBase.OnPropertyChanged (System.ComponentModel.PropertyChangedEventArgs args) [0x00000] in d:\a\1\s\Source\Prism\Mvvm\BindableBase.cs:99
at Prism.Mvvm.BindableBase.OnPropertyChanged (System.String propertyName) [0x00000] in d:\a\1\s\Source\Prism\Mvvm\BindableBase.cs:90
at Prism.Mvvm.BindableBase.RaisePropertyChanged (System.String propertyName) [0x00000] in d:\a\1\s\Source\Prism\Mvvm\BindableBase.cs:76
-->at ProjectMobile.ViewModels.DarFormPageViewModel.set_EquipmentNumberList (System.Collections.ObjectModel.ObservableCollection`1[T] value) [0x00008] in /Users/vpo1/Project-Mobile/MobileApp/ProjectMobile.ViewModels/PhFormPageViewModel:104 <--
at ProjectMobile.ViewModels.DarFormPageViewModel.PopulateEquipmentList (System.Boolean IsFromNavigatedTo) [0x00013] in /Users/vpo1/Project-Mobile/MobileApp/ProjectMobile.ViewModels/PhFormPageViewModel:940
at ProjectMobile.ViewModels.DarFormPageViewModel.DeliveryItemChangedCommandHandler (System.String obj) [0x00008] in /Users/vpo1/Project-Mobile/MobileApp/ProjectMobile.ViewModels/PhFormPageViewModel:61
at Prism.Commands.DelegateCommand`1[T].Execute (T parameter) [0x00000] in d:\a\1\s\Source\Prism\Commands\DelegateCommand{T}.cs:82
at Prism.Commands.DelegateCommand`1[T].Execute (System.Object parameter) [0x00000] in d:\a\1\s\Source\Prism\Commands\DelegateCommand{T}.cs:103
at Prism.Commands.DelegateCommandBase.System.Windows.Input.ICommand.Execute (System.Object parameter) [0x00000] in d:\a\1\s\Source\Prism\Commands\DelegateCommandBase.cs:62
at Xamarin.CommunityToolkit.Behaviors.EventToCommandBehavior.OnTriggerHandled (System.Object sender, System.Object eventArgs) [0x00042] in <823f4019dc404dea9e9331c7114e4863>:0
at Xamarin.Forms.Picker.OnSelectedIndexChanged (System.Object bindable, System.Object oldValue, System.Object newValue) [0x00012] in D:\a\1\s\Xamarin.Forms.Core\Picker.cs:283
at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent) [0x00120] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:512
at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x00173] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:446
at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, System.Boolean fromStyle, System.Boolean checkAccess) [0x0004d] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:374
at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:349
at Xamarin.Forms.Picker.set_SelectedIndex (System.Int32 value) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Picker.cs:114
at Xamarin.Forms.Picker.UpdateSelectedIndex (System.Object selectedItem) [0x00008] in D:\a\1\s\Xamarin.Forms.Core\Picker.cs:296
at Xamarin.Forms.Picker.OnSelectedItemChanged (Xamarin.Forms.BindableObject bindable, System.Object oldValue, System.Object newValue) [0x00006] in D:\a\1\s\Xamarin.Forms.Core\Picker.cs:289
at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent) [0x00120] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:512
at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x00173] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:446
at Xamarin.Forms.BindingExpression.ApplyCore (System.Object sourceObject, Xamarin.Forms.BindableObject target, Xamarin.Forms.BindableProperty property, System.Boolean fromTarget) [0x00226] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:160
at Xamarin.Forms.BindingExpression.Apply (System.Boolean fromTarget) [0x0003e] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:56
at Xamarin.Forms.BindingExpression+BindingExpressionPart.<PropertyChanged>b__49_0 () [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:762
at Xamarin.Forms.BindingExpression+BindingExpressionPart.PropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs args) [0x000cb] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:773
at Xamarin.Forms.BindingExpression+WeakPropertyChangedProxy.OnPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e) [0x00012] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:666
at (wrapper delegate-invoke) <Module>.invoke_void_object_PropertyChangedEventArgs(object,System.ComponentModel.PropertyChangedEventArgs)
at Prism.Mvvm.BindableBase.OnPropertyChanged (System.ComponentModel.PropertyChangedEventArgs args) [0x00000] in d:\a\1\s\Source\Prism\Mvvm\BindableBase.cs:99
at Prism.Mvvm.BindableBase.OnPropertyChanged (System.String propertyName) [0x00000] in d:\a\1\s\Source\Prism\Mvvm\BindableBase.cs:90
at Prism.Mvvm.BindableBase.RaisePropertyChanged (System.String propertyName) [0x00000] in d:\a\1\s\Source\Prism\Mvvm\BindableBase.cs:76
at ProjectMobile.ViewModels.DarFormPageViewModel.set_BolNumberText (System.String value) [0x00008] in /Users/vpo1/Project-Mobile/MobileApp/ProjectMobile.ViewModels/PhFormPageViewModel:118
at ProjectMobile.ViewModels.DarFormPageViewModel.OnNavigatedTo (Prism.Navigation.INavigationParameters parameters) [0x00170] in /Users/vpo1/Project-Mobile/MobileApp/ProjectMobile.ViewModels/PhFormPageViewModel:863
Solution 1:[1]
Removing this part of the code should resolve the problem:
<xct:EventToCommandBehavior
EventName="SelectedIndexChanged"
Command="{Binding ItemChangedCommand}"
CommandParameter="{Binding Source={x:Reference Picker1},
Path=SelectedItem}"/>
This can be done in many other ways, like using the CodeNumberText property to handle it. As it is likely a bug in that open source component you can submit the bug to their GitHub or fix it yourself.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ivan IÄin |
