2013-12-08 41 views
1

我在WPF上編寫了一個簡單的窗體,它工作正常。但現在我應該將目標框架從4改爲3.5。但是當我運行3.5版本時,它與excepton一起失敗。所以OnLoaded是這樣的:當隱藏窗口時出現InvalidOperationException

private void MainWindow_OnLoaded(object sender, RoutedEventArgs e) 
    { 
     _notificator = new Notificator(Directory.GetCurrentDirectory()); 
     _notificator.EventStarted += (o, ev) => Dispatcher.Invoke(new Action(() => 
                     { 
                      new NotificationPage(ev).Show(); 
                      _collection.Remove(ev); 
                     })); 
     _notificator.EventRemoved += (o, ev) => _collection.Remove(ev); 
     _notificator.EventAdded += (o, ev) => _collection.Insert(_notificator.Events.IndexOf(ev), ev); 
     _collection = new ObservableCollection<MyEvent>(_notificator.Events); 
     EventsGrid.DataContext = _collection; 
     _notificator.Start(); 
     LostEventsGrid.DataContext = _notificator.LostEvents; 
     Hide(); 
    } 

我寫了整個方法,但它僅在具有Two-way binding requires Path or XPath隱藏方法失敗。堆棧跟蹤:

вSystem.Windows.Data.BindingExpression.CreateBindingExpression(DependencyObject的d,的DependencyProperty DP,裝訂裝訂,BindingExpressionBase父) вSystem.Windows.Data.Binding.CreateBindingExpressionOverride(DependencyObject的目標,的DependencyProperty DP,BindingExpressionBase所有者) вSystem.Windows.Data.BindingOperations.SetBinding(DependencyObject的目標,的DependencyProperty DP,BindingBase結合) вMicrosoft.Windows.Controls.DataGridBoundColumn.ApplyBinding(DependencyObject的目標,的DependencyProperty屬性) вMicrosoft.Windows.Controls.DataGridTextColumn .GenerateElement(DataGridCell cell,Object dataItem) Microso (Object item,DataGridRow ownerRow,Object DataGridCell.BuildVisualTree())中的Microsoft.Windows.Controls.DataGridCell.BuildVisualTree()中的布爾isEditing,Object dataItem,DataGridCell cell) Int32索引)Microsoft.Windows.Controls.DataGridCell.PrepareCell(Object item,ItemsControl cellsPresenter,DataGridRow ownerRow)在System.Windows中的Microsoft.Windows.Controls.Primitives.DataGridCellsPresenter.PrepareContainerForItemOverride(DependencyObject元素,對象項目) 。 Controls.ItemsControl.MS.Internal.Controls.GeeneratorHost.PrepareItemContainer(DependencyObject container,Object item)System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.PrepareItemContainer(Dependency對象容器) вMicrosoft.Windows.Controls.DataGridCellsPanel.InsertContainer(的Int32 childIndex,容器的UIElement,布爾isRecycled) вMicrosoft.Windows.Controls.DataGridCellsPanel.InsertNewContainer(的Int32 childIndex,容器的UIElement) вMicrosoft.Windows.Controls。 DataGridCellsPanel.AddContainerFromGenerator(的Int32 childIndex,孩子的UIElement,布爾newlyRealized) вMicrosoft.Windows.Controls.DataGridCellsPanel.GenerateChild(IItemContainerGenerator發電機,尺寸約束,列的DataGridColumn,的Int32 & childIndex,大小& childSize) вMicrosoft.Windows.Controls。 DataGridCellsPanel.GenerateChildren(IItemContainerGenerator生成器,Int32 startIndex,Int32 endIndex,大小約束) Microsoft.Windows.Controls。 DataGridCellsPanel.GenerateAndMeasureChildrenForRealizedColumns(大小限制) вMicrosoft.Windows.Controls.DataGridCellsPanel.MeasureOverride(大小限制) вSystem.Windows.FrameworkElement.MeasureCore(尺寸availableSize) вSystem.Windows.UIElement.Measure(尺寸availableSize) в MS.Internal.Helper.MeasureElementWithSingleChild(UIElement元素,Size約束)System.Windows.Controls.ItemsPresenter.MeasureOverride(Size constraint) System.Windows.FrameworkElement.MeasureCore(Size availableSize) System.Windows.UIElement。在Microsoft.Windows.Controls.Primitives.DataGridCellsPresenter中測量(Size availableSize) System.Windows.Controls.Control.MeasureOverride(Size constraint) .MeasureOverride(尺碼availableSize) вSystem.Windows.FrameworkElement.MeasureCore(尺碼availableSize) вSystem.Windows.UIElement.Measure(尺碼availableSize) вSystem.Windows.Controls.Grid.MeasureCell(的Int32細胞,布爾forceInfinityV) System.Windows中的 。System.Windows.Controls.Grid.MeasureOverride(Size約束)中的Controls.Grid.MeasureCellsGroup(Int32 cellsHead,Size referenceSize,Boolean ignoreDesiredSizeU,Boolean forceInfinityV) System.Windows.FrameworkElement.MeasureCore(Size availableSize) System。 Windows.UIElement.Measure(Size availableSize) System.Windows.Controls.Border.MeasureOverride(Size constraint) System.Windows.FrameworkElement.MeasureCore(Size availableSize) System.Windows.UIElement.Measure(Size availableSize) вSystem.Windows.Controls.Control.MeasureOverride(尺寸約束) вSystem.Windows.FrameworkElement.MeasureCore(尺碼availableSize) вSystem.Windows.UIElement.Measure(尺碼availableSize) вSystem.Windows.Controls.VirtualizingStackPanel.MeasureOverride(尺寸約束) вMicrosoft.Windows.Controls.Primitives.DataGridRowsPresenter.MeasureOverride(尺寸約束) вSystem.Windows.FrameworkElement.MeasureCore(尺碼availableSize) вSystem.Windows .UIElement.Measure(尺碼availableSize) вSystem.Windows.ContextLayoutManager.UpdateLayout() вSystem.Windows.Interop.HwndSource.Process_WM_SIZE(的UIElement rootUIElement,IntPtr的HWND,MSG的Int32,IntPtr的wParam中,IntPtr的LPARAM) в系統。 Windows.Interop.HwndSource.LayoutFilterMessage(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,布爾&處理) вMS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean &處理) вMS.Win32.HwndSubclass.DispatcherCallbackOperation(對象O) вSystem.Windows.Threading.ExceptionWrapper.InternalRealCall(代表回調,對象指定參數時,布爾isSingleParameter) вSystem.Windows.Threading.ExceptionWrapper.TryCatchWhen(對象來源,委託回調,Object args,Boolean isSingleParameter,Delegate catchHandler)System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback,Object args,Boolean isSingleParameter,Delegate catchHandler)System.Windows.Threading.Dispatcher.WrappedInvoke代理回調,Object args,Boolean isSingleParameter)System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority,TimeSpan timeout,Delegate method,Object args,Boolean isSingleParameter)MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam)中的System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority,Delegate method,Object arg).CallWindowProc(IntPtr的WNDPROC,IntPtr的的hWnd,MSG的Int32,IntPtr的wParam中,IntPtr的LPARAM) вMS.Win32.HwndSubclass.DefWndProcWrapper(IntPtr的HWND,MSG的Int32,IntPtr的wParam中,IntPtr的LPARAM) вMS.Win32.UnsafeNativeMethods.CallWindowProc( IntPtr的WNDPROC,IntPtr的的hWnd,MSG的Int32,IntPtr的wParam中,IntPtr的LPARAM) вMS.Win32.HwndSubclass.CallOldWindowProc(IntPtr的oldWndProc,IntPtr的HWND,MSG的Int32,IntPtr的wParam中,IntPtr的LPARAM) вMS.Win32.HwndSubclass.SubclassWndProc( IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam)

在.NET

4+它工作正常

回答

0

解決:問題是在WPFToolkit,因爲使用DataGrid的,即不存在WPF3.5的。所以我已經參考了它,當使用來自WPFToolkit而不是內部框架的控件時,我仍然在.Net 4+上遇到錯誤。

+0

嗨,你能解釋一下你引用的正是組裝和版本來解決這個問題? – MoMo

+0

@MoMo您應該只使用.Net 4.0+並且不要從任何項目中引用WPFToolkit。在我的情況下,它正在工作。 –

1

對於可能有此問題的其他人。我的問題是我是如何設置我的綁定路徑。我正在鎖定自己的路線,並且每當鏈條發生變化時,控件就會失去綁定的能力並拋出異常。

考慮例如:

<Binding Path="EntityProperty.Name" Mode="TwoWay"> 

每當EntityProperty參考變化則控制將失去它的結合路徑和拋出異常。

請參見下面的後幾個解決方法:https://stackoverflow.com/a/6250988/176728