【问题标题】:InvalidOperationException when hide window隐藏窗口时出现 InvalidOperationException
【发布时间】:2013-12-25 12:14:33
【问题描述】:

我正在 WPF 上编写一个简单的表单,它工作正常。但现在我应该将目标框架从 4 更改为 3.5。但是当我运行 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 的隐藏方法上失败。堆栈跟踪:

в System.Windows.Data.BindingExpression.CreateBindingExpression(DependencyObject d, DependencyProperty dp, Binding binding, BindingExpressionBase parent) в 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 单元格,对象数据项) в Microsoft.Windows.Controls.DataGridColumn.BuildVisualTree(Boolean isEditing, Object dataItem, DataGridCell cell) ? Microsoft.Windows.Controls.DataGridCell.BuildVisualTree() в Microsoft.Windows.Controls.DataGridCell.PrepareCell(对象项,DataGridRow ownerRow,Int32 索引) в Microsoft.Windows.Controls.DataGridCell.PrepareCell(对象项,ItemsControl cellsPresenter,DataGridRow ownerRow) в Microsoft.Windows.Controls.Primitives.DataGridCellsPresenter.PrepareContainerForItemOverride(DependencyObject 元素,对象项) • System.Windows.Controls.ItemsControl.MS.Internal.Controls.IGeneratorHost.PrepareItemContainer(DependencyObject 容器,对象项) • System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.PrepareItemContainer(DependencyObject 容器) в Microsoft.Windows.Controls.DataGridCellsPanel.InsertContainer(Int32 childIndex, UIElement container, Boolean isRecycled) в Microsoft.Windows.Controls.DataGridCellsPanel.InsertNewContainer(Int32 childIndex, UIElement container) в Microsoft.Windows.Controls.DataGridCellsPanel.AddContainerFromGenerator(Int32 childIndex, UIElement child, Boolean newRealized) в Microsoft.Windows.Controls.DataGridCellsPanel.GenerateChild(IItemContainerGenerator 生成器,大小约束,DataGridColumn 列,Int32& childIndex,Size& 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(Size availableSize) в System.Windows.UIElement.Measure(Size availableSize) в MS.Internal.Helper.MeasureElementWithSingleChild(UIElement 元素,大小约束) в System.Windows.Controls.ItemsPresenter.MeasureOverride(大小约束) в System.Windows.FrameworkElement.MeasureCore(Size availableSize) в System.Windows.UIElement.Measure(Size availableSize) в System.Windows.Controls.Control.MeasureOverride(大小约束) в Microsoft.Windows.Controls.Primitives.DataGridCellsPresenter.MeasureOverride(Size availableSize) в System.Windows.FrameworkElement.MeasureCore(Size availableSize) в System.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(大小约束) в 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(大小约束) в System.Windows.FrameworkElement.MeasureCore(Size availableSize) в System.Windows.UIElement.Measure(Size availableSize) в System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(大小约束) в Microsoft.Windows.Controls.Primitives.DataGridRowsPresenter.MeasureOverride(大小约束) в 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, IntPtr wParam, IntPtr lParam) в System.Windows.Interop.HwndSource.LayoutFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& 处理) в MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& 处理) в MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) в System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,对象参数,布尔isSingleParameter) в System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象源,委托回调,对象参数,布尔isSingleParameter,委托catchHandler) в System.Windows.Threading.Dispatcher.WrappedInvoke(委托回调,对象参数,布尔isSingleParameter,委托catchHandler) в System.Windows.Threading.Dispatcher.WrappedInvoke(委托回调,对象参数,布尔isSingleParameter) в System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority 优先级,TimeSpan 超时,委托方法,对象参数,布尔 isSingleParameter) в System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority 优先级,委托方法,对象 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+ 中运行良好

【问题讨论】:

    标签: c# .net wpf windows .net-3.5


    【解决方案1】:

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

    考虑这个例子:

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

    每当EntityProperty 引用发生变化时,控件就会失去它的绑定路径并抛出异常。

    请参阅以下帖子了解一些解决方法:https://stackoverflow.com/a/6250988/176728

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 您好,您能具体解释一下您解决此问题时引用的程序集和版本吗?
      • @MoMo 您只应该使用 .Net 4.0+ 并且不要从任何项目中引用 WPFToolkit。就我而言,它正在工作。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-19
      相关资源
      最近更新 更多