在创建新选项卡后切换选项卡时抛出System.ArgumentException

时间:2014-02-27 22:30:05

标签: c# .net wpf telerik caliburn.micro

我们发现在通过Windows Update安装.Net 4.5.1之后,会引入以下错误。我们当前的解决方案,不会持续很长时间,是卸载更新,并将每个人的机器保持在.Net 4.0。

我们有一个WPF屏幕,用于运行和查看报告。每个报告都是一个" ReportViewModel。"视图模型的集合在" ReportManagerViewModel"管理使用Caliburn.Micro.Conductor的标签集合。

如果我们打开现有标签,则标签会完美加载。然后,我们启动一个新选项卡,运行一个报告,将焦点放在新选项卡上并填充其数据。此选项卡现在是选项卡集合的一部分。如果我们尝试切换回其中一个已打开的选项卡,则会发生以下情况:

第一次单击(第一次单击预先存在的选项卡)不执行任何操作。第二次单击会使应用程序崩溃,并显示以下错误:

  

System.ArgumentException:已添加具有相同键的项目。

有趣的是,当切换活动标签时,会调用Conductor.ActivateItem()。抛出错误时,甚至不会调用此方法。

使用Visual Studio 2010,Caliburn.Micro v.1.5.2,Telerik WPF控制2013年第3季度热修复1424.

编辑:我们现在知道Telerik不是原因,因为我们从RadTabItem切换到TabItem,而我们没有使用Caliburn.Micro.Conductor。

  

例外:   已添加具有相同键的项目。   消息来源:mscorlib   堆栈跟踪:      在System.ThrowHelper.ThrowArgumentException(ExceptionResource资源)      在System.Collections.Generic.Dictionary 2.Insert(TKey key, TValue value, Boolean add) at System.Collections.Generic.Dictionary 2..ctor(IDictionary 2 dictionary, IEqualityComparer 1比较器)      在System.Windows.Controls.Primitives.Selector.InternalSelectedItemsStorage..ctor(InternalSelectedItemsStorage集合,IEqualityComparer`1 equalityComparer)      在System.Windows.Controls.Primitives.Selector.SelectionChanger.ApplyCanSelectMultiple()      在System.Windows.Controls.Primitives.Selector.SelectionChanger.End()      在System.Windows.Controls.Primitives.Selector.SetSelectedHelper(Object item,FrameworkElement UI,Boolean selected)      在System.Windows.Controls.Primitives.Selector.NotifyIsSelectedChanged(FrameworkElement容器,布尔选中,RoutedEventArgs e)      在System.Windows.Controls.Primitives.Selector.OnSelected(Object sender,RoutedEventArgs e)      在System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象目标,RoutedEventArgs routedEventArgs)      at System.Windows.EventRoute.InvokeHandlersImpl(Object source,RoutedEventArgs args,Boolean reRaised)      在System.Windows.UIElement.RaiseEventImpl(DependencyObject sender,RoutedEventArgs args)      在System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)      在System.Windows.Controls.TabItem.OnSelected(RoutedEventArgs e)      在System.Windows.Controls.TabItem.OnIsSelectedChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)      在System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)      在System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)      在System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)      在System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex,DependencyProperty dp,PropertyMetadata metadata,EffectiveValueEntry oldEntry,EffectiveValueEntry& newEntry,Boolean coerceWithDeferredReference,Boolean coerceWithCurrentValue,OperationType operationType)      在System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp,Object value,PropertyMetadata metadata,Boolean coerceWithDeferredReference,Boolean coerceWithCurrentValue,OperationType operationType,Boolean isInternal)      在System.Windows.DependencyObject.SetCurrentValueInternal(DependencyProperty dp,Object value)      在System.Windows.Controls.TabItem.OnPreviewGotKeyboardFocus(KeyboardFocusChangedEventArgs e)      在System.Windows.UIElement.OnPreviewGotKeyboardFocusThunk(Object sender,KeyboardFocusChangedEventArgs e)      在System.Windows.Input.KeyboardFocusChangedEventArgs.InvokeEventHandler(Delegate genericHandler,Object genericTarget)      在System.Windows.RoutedEventArgs.InvokeHandler(委托处理程序,对象目标)      在System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象目标,RoutedEventArgs routedEventArgs)      at System.Windows.EventRoute.InvokeHandlersImpl(Object source,RoutedEventArgs args,Boolean reRaised)      在System.Windows.UIElement.RaiseEventImpl(DependencyObject sender,RoutedEventArgs args)      在System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)      在System.Windows.UIElement.RaiseEvent(RoutedEventArgs args,布尔值信任)      在System.Windows.Input.InputManager.ProcessStagingArea()      在System.Windows.Input.InputManager.ProcessInput(InputEventArgs输入)      at System.Windows.Input.KeyboardDevice.TryChangeFocus(DependencyObject newFocus,IKeyboardInputProvider keyboardInputProvider,Boolean askOld,Boolean askNew,Boolean forceToNullIfFailed)      在System.Windows.Input.KeyboardDevice.Focus(DependencyObject focus,Boolean askOld,Boolean askNew,Boolean forceToNullIfFailed)      在System.Windows.Input.KeyboardDevice.Focus(IInputElement元素)      在System.Windows.UIElement.Focus()      在System.Windows.Controls.TabItem.SetFocus()      在System.Windows.Controls.TabItem.OnMouseLeftButtonDown(MouseButtonEventArgs e)      在System.Windows.UIElement.OnMouseLeftButtonDownThunk(Object sender,MouseButtonEventArgs e)      在System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler,Object genericTarget)      在System.Windows.RoutedEventArgs.InvokeHandler(委托处理程序,对象目标)      在System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象目标,RoutedEventArgs routedEventArgs)      at System.Windows.EventRoute.InvokeHandlersImpl(Object source,RoutedEventArgs args,Boolean reRaised)      在System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender,RoutedEventArgs args,RoutedEvent newEvent)      在System.Windows.UIElement.OnMouseDownThunk(Object sender,MouseButtonEventArgs e)      在System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler,Object genericTarget)      在System.Windows.RoutedEventArgs.InvokeHandler(委托处理程序,对象目标)      在System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象目标,RoutedEventArgs routedEventArgs)      at System.Windows.EventRoute.InvokeHandlersImpl(Object source,RoutedEventArgs args,Boolean reRaised)      在System.Windows.UIElement.RaiseEventImpl(DependencyObject sender,RoutedEventArgs args)      在System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)      在System.Windows.UIElement.RaiseEvent(RoutedEventArgs args,布尔值信任)      在System.Windows.Input.InputManager.ProcessStagingArea()      在System.Windows.Input.InputManager.ProcessInput(InputEventArgs输入)      在System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)      在System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd,InputMode模式,Int32时间戳,RawMouseActions操作,Int32 x,Int32 y,Int32轮)      在System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd,WindowMessage msg,IntPtr wParam,IntPtr lParam,Boolean& processed)      在System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean& processed)      在MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean& handling)      在MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)      在System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,对象args,Int32 numArgs)      在MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source,Delegate方法,Object args,Int32 numArgs,Delegate catchHandler)      在System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority,TimeSpan timeout,Delegate方法,Object args,Int32 numArgs)      在MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam)      在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)      在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 reason,Int32 pvLoopData)      在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context)      在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context)      在System.Windows.Forms.Application.Run(Form mainForm)      在[AppName] .Windows.Program.Main()在M:\ Projects [AppName] \ Windows \ Program.cs:第129行

2 个答案:

答案 0 :(得分:1)

http://support.microsoft.com/kb/2909048

TabViewModel的“GetHashCode()”和“Equals()”方法是问题的根源。这些将需要重写或删除。

答案 1 :(得分:0)

只是对接受的答案进行详细阐述。当我更改绑定值并重新选择项目时(在选择时加上DataTemplate交换),我有一个与ListBox相同的ArgumentException。以下内容适用于我(当然,您必须确保索引值在列表中是唯一的):

        public override bool Equals(object obj)
        {
            if (obj is ExtendedButton2Content)
            {
                ExtendedButton2Content temp = (ExtendedButton2Content)obj;
                return temp.Index == this.Index;
            }
            else
            {
                return false;
            }
        }

        public override int GetHashCode()
        {
            return Index;
        }