My Windows Phone application crashes when creating a new instance of binded list
BindedList Change:
OsyId = Response.recordedBills.Count > 50 ? new List<RecordedBillDetailViewModel>(Response.recordedBills.Take(50)) : new List<RecordedBillDetailViewModel>(Response.recordedBills);
MultiSelector:
<toolkit:LongListMultiSelector ItemsSource="{Binding OsyId}" x:FieldModifier="public" x:Name="longList" EnforceIsSelectionEnabled="{Binding EnforceSelectionEnable}" Margin="0,0,10,0" SelectionChanged="LongListMultiSelector_SelectionChanged" IsSelectionEnabledChanged="LongListMultiSelector_IsSelectionEnabledChanged" MaxHeight="600" Grid.Row="1" HorizontalContentAlignment="Stretch">
Crash Report:
{System.ArgumentException: Value does not fall within the expected range.
at MS.Internal.XcpImports.MethodEx(IntPtr ptr, String name, CValue[] cvData)
at MS.Internal.XcpImports.MethodPack(IntPtr objectPtr, String methodName, Object[] rawData)
at MS.Internal.XcpImports.UIElement_TransformToVisual(UIElement element, UIElement visual)
at System.Windows.UIElement.TransformToVisual(UIElement visual)
at Microsoft.Phone.Controls.LongListMultiSelector.ApplyItemsState(State state, Boolean useTransitions)
at Microsoft.Phone.Controls.LongListMultiSelector.OnIsSelectionEnabledChanged(DependencyPropertyChangedEventArgs e)
at Microsoft.Phone.Controls.LongListMultiSelector.OnIsSelectionEnabledPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue)
at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet)
at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
at Microsoft.Phone.Controls.LongListMultiSelector.set_IsSelectionEnabled(Boolean value)
at Microsoft.Phone.Controls.LongListMultiSelector.UpdateIsSelectionEnabled()
at Microsoft.Phone.Controls.LongListMultiSelector.OnSelectionChanged(IList removedItems, IList addedItems)
at Microsoft.Phone.Controls.LongListMultiSelector.OnSelectedItemsCollectionCleared(Object sender, ClearedChangedArgs e)
at Microsoft.Phone.Controls.LongListMultiSelector.SelectedItemsList.ClearItems()
at System.Collections.ObjectModel.Collection1.Clear()
at Microsoft.Phone.Controls.LongListMultiSelector.OnItemsSourceChanged(Object oldValue, Object newValue)
at Microsoft.Phone.Controls.LongListMultiSelector.OnItemsSourcePropertyChanged(Object sender, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue)
at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
at System.Windows.DependencyObject.RefreshExpression(DependencyProperty dp)
at System.Windows.Data.BindingExpression.SendDataToTarget()
at System.Windows.Data.BindingExpression.SourcePropertyChanged(PropertyPathListener sender, PropertyPathChangedEventArgs args)
at System.Windows.PropertyPathListener.RaisePropertyPathStepChanged(PropertyPathStep source)
at System.Windows.PropertyAccessPathStep.RaisePropertyPathStepChanged(PropertyListener source)
at System.Windows.CLRPropertyListener.SourcePropertyChanged(Object sender, PropertyChangedEventArgs args)
at System.Windows.Data.WeakPropertyChangedListener.PropertyChangedCallback(Object sender, PropertyChangedEventArgs args)
at System.ComponentModel.PropertyChangedEventHandler.Invoke(Object sender, PropertyChangedEventArgs e)
at Windows8.Common.Helpers.NotifyPropertyChanged.OnPropertyChanged(String propertyName)
at Windows8.Common.Helpers.NotifyPropertyChanged.SetProperty[T](T& storage, T value, String propertyName)
at Windows8.Common.ViewModels.BillPaymentViewModel.set_OsyId(List`1 value)
at Windows8.Common.ViewModels.BillPaymentViewModel.<InitiateBillRequestToRecordedBill>d__ee.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Windows8.Common.ViewModels.BillPaymentViewModel.<SetRecordedBillsAction>d__71.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Windows8.Common.ViewModels.CompanyMenu.<BillPaymentSubScreenAction>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Windows8.Common.Helpers.DelegateCommandAsync.<System.Windows.Input.ICommand.Execute>d__0.MoveNext()}
How can I solve it?
Instead of creating a new instance try to clear and add items to the list. It might help solve the problem. This is assuming OsyId
is a ObservableCollection<RecordedBillDetailViewModel>
var itemsToAdd = Response.recordedBills.Take(50); // Take 50 is fine even when the response does not have 50 items. Need not add a contion for it.
OsyId.Clear();
foreach(var item in itemsToAdd)
OsyId.Add(item)
Thanks, Shubhan