首页 文章

隐藏窗口时出现InvalidOperationException

提问于
浏览
1

我'm writing a simple form on WPF, and it works fine. But now I should change target framework from 4 to 3.5. But when i' m运行3.5版本,它失败了除了 . 所以 OnLoaded 是这样的:

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 的Hide方法时失败 . 堆栈跟踪:

вSystem.Windows.Data.BindingExpression.CreateBindingExpression(DependencyObject d,DependencyProperty dp,Binding binding,BindingExpressionBase parent) - System.Windows.Data.Binding.CreateBindingExpressionOverride(DependencyObject target,DependencyProperty dp,BindingExpressionBase owner) - 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)•Microsoft .Windows.Controls.DataGridColumn.BuildVisualTree(Boolean isEditing,Object dataItem,DataGridCell cell) - Microsoft.Windows.Controls.DataGridCell.BuildVisualTree() - Microsoft.Windows.Controls.DataGridCell.PrepareCell(Object item,DataGridRow ownerRow,Int32 index) Microsoft.Windows.Controls.DataGridCell.PrepareCell(Object item,ItemsControl cel lsPresenter,DataGridRow ownerRow)Microsoft.Windows.Controls.Primitives.DataGridCellsPresenter.PrepareContainerForItemOverride(DependencyObject element,Object item)вSystem.Windows.Controls.ItemsControl.MS.Internal.Controls.IGeneratorHost.PrepareItemContainer(DependencyObject container,Object item) System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.PrepareItemContainer(DependencyObject容器) - Microsoft.Windows.Controls.DataGridCellsPanel.InsertContainer(Int32 childIndex,UIElement容器,Boolean isRecycled) - Microsoft.Windows.Controls . DataGridCellsPanel.InsertNewContainer(Int32 childIndex,UIElement容器) - Microsoft.Windows.Controls.DataGridCellsPanel.AddContainerFromGenerator(Int32 childIndex,UIElement child,BooleannewRealized) - Microsoft.Windows.Controls.DataGridCellsPanel.GenerateChild(IItemContainerGenerator generator,Size constraint,DataGridColumn column, Int32&childIndex,Size&childSi ze.Microsoft.Windows.Controls.DataGridCellsPanel.GenerateChildren(IItemContainerGenerator generator,Int32 startIndex,Int32 endIndex,Size constraint)вMicrosoft.Windows.Controls.DataGridCellsPanel.GenerateAndMeasureChildrenForRealizedColumns(Size constraint)вMicrosoft.Windows.Controls.DataGridCellsPanel.MeasureOverride(大小约束)вSystem.Windows.FrameworkElement.MeasureCore(Size availableSize)вSystem.Windows.UIElement.Measure(Size availableSize)вMS.Internal.Helper.MeasureElementWithSingleChild(UIElement element,Size constraint)вSystem.Windows.Controls.ItemsPresenter .MeasureOverride(大小约束)вSystem.Windows.FrameworkElement.MeasureCore(Size availableSize)вSystem.Windows.UIElement.Measure(Size availableSize)вSystem.Windows.Controls.Control.MeasureOverride(Size constraint)вMicrosoft.Windows.Controls .Primitives.DataGridCellsPresenter.MeasureOverride(Size availableSize)вSystem.Windows.FrameworkElement.MeasureCore(Size availableSize)вSyst em.Windows.UIElement.Measure(Size availableSize)вSystem.Windows.Controls.Grid.MeasureCell(Int32 cell,Boolean forceInfinityV)вSystem.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead,Size referenceSize,Boolean ignoreDesiredSizeU,Boolean forceInfinityV )System.Windows.Controls.Grid.MeasureOverride(Size constraint)вSystem.Windows.FrameworkElement.MeasureCore(Size availableSize)вSystem.Windows.UIElement.Measure(Size availableSize)вSystem.Windows.Controls.Border.MeasureOverride(大小约束)вSystem.Windows.FrameworkElement.MeasureCore(Size availableSize)вSystem.Windows.UIElement.Measure(Size availableSize)вSystem.Windows.Controls.Control.MeasureOverride(Size constraint)вSystem.Windows.FrameworkElement.MeasureCore( Size availableSize)вSystem.Windows.UIElement.Measure(Size availableSize)вSystem.Windows.Controls.VirtualizingStackPanel.MeasureOverride(Size constraint)вMicrosoft.Windows.Controls.Primitives.DataGridRowsPresenter.MeasureOverride(S ize约束)вSystem.Windows.FrameworkElement.MeasureCore(Size availableSize)вSystem.Windows.UIElement.Measure(Size availableSize)вSystem.Windows.ContextLayoutManager.UpdateLayout()вSystem.Windows.Interop.HwndSource.Process_WM_SIZE(UIElement rootUIElement ,IntPtr hwnd,Int32 msg,IntPtrwParam,IntPtr lParam)вSystem.Windows.Interop.HwndSource.LayoutFilterMessage(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean&handling)в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(Delegate callback,Object args,Boolean isSingleParameter)вSystem.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source ,Delegate callback,Object args,Boolean isSingleParameter,Delegate catchHandler)System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback,Object args,Boolean isSingleParameter,Delegate catchHandler)вSystem.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args,Boolean isSingleParameter)System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority,TimeSpan timeout,Delegate方法,Object args,Boolean isSingleParameter)вS ystem.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority,Delegate方法,Object arg)•MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam)•MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr) wndProc,IntPtr hWnd,Int32 msg,IntPtr wParam,IntPtr lParam)•MS.Win32.HwndSubclass.DefWndProcWrapper(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam)•MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc,IntPtr hWnd, Int32 msg,IntPtr wParam,IntPtr lParam)•MS.Win32.HwndSubclass.CallOldWindowProc(IntPtr oldWndProc,IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam)•MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,Int32 msg,IntPtr wParam ,IntPtr lParam)

在.net 4它工作正常

2 回答

  • 1

    对于可能有这个问题的其他人 . 我的问题是我是如何设置绑定路径的 . 我正在链接我的路径,每当链上的任何引用发生变化时,控件都会失去绑定能力并抛出 Two-way binding requires Path or XPath 异常 .

    考虑这个例子:

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

    只要 EntityProperty 引用发生更改,控件就会松开它的绑定路径并抛出异常 .

    有关解决方法,请参阅以下帖子:https://stackoverflow.com/a/6250988/176728

  • 0

    解决:问题出现在WPFToolkit中,因为使用了DataGrid,WPF3.5中不存在 . 所以我引用了它,当使用WPFToolkit而不是内部框架的控件时,我仍然会在.Net 4上遇到错误 .

相关问题