首页 > 解决方案 > 当我将鼠标悬停得太快时,FixedPage 会导致 ArgumentNullException

问题描述

我遇到的情况是 WPF 在我的一个视图上成功创建了一个 Image 元素,但似乎需要一两秒钟才能正确设置该元素的逻辑父级。如果我将鼠标悬停在那个时间窗口中,我的应用程序会由于来自 LogicalTreeHelper 的 ArgumentNullException 而崩溃。

如果我等待第二个左右,一切正常,没有问题。但我很难要求我的用户这样做。

请注意,这只发生在我的 UserControl 位于 DocumentViewer 显示的 FixedDocument 中的 FixedPage 中时。当我在其他上下文中使用相同的控件时(远离 FixedDocument 等),它可以正常工作。

有谁知道这个“窗口”或可能是什么原因?

这是 FixedPage.cs 中试图获取我的项目的父项的代码。我已经添加了评论。

    internal int[] _CreateChildIndex(DependencyObject e)
    {
        ArrayList childPath = new ArrayList();
        while (e != this)
        { 
            // *** 'e' is an Image control, created by my view ***
            // *** This value 'parent' is NULL if I mouse over too quickly ***

            DependencyObject parent = LogicalTreeHelper.GetParent(e);
            int childIndex = -1;
            if (parent is FixedPage)
            {
                childIndex = ((FixedPage)parent).Children.IndexOf((UIElement)e);
            }
            else if (parent is Canvas)
            {
                childIndex = ((Canvas)parent).Children.IndexOf((UIElement)e);
            }
            else
            {
                // *** Because 'parent' is null, we end up here.  This call throws ***
                IEnumerable currentChildrens = LogicalTreeHelper.GetChildren(parent);

这是抛出的代码

public static IEnumerable GetChildren(DependencyObject current)
{
    if (current == null)
    {
        throw new ArgumentNullException("current"); 
    }

这是鼠标悬停的调用堆栈。除了 App.Run 方法,这里没有我的代码。

System.Windows.LogicalTreeHelper.GetChildren    C#
System.Windows.Documents.FixedPage._CreateChildIndex    C#
System.Windows.Documents.FixedTextView.GetTextPositionFromPoint C#
MS.Internal.Documents.TextViewBase.System.Windows.Documents.ITextView.GetTextPositionFromPoint  C#
MS.Internal.Documents.DocumentPageTextView.GetTextPositionFromPoint C#
MS.Internal.Documents.MultiPageTextView.GetTextPositionFromPoint    C#
MS.Internal.Documents.TextViewBase.System.Windows.Documents.ITextView.GetTextPositionFromPoint  C#
System.Windows.Documents.TextEditorMouse.IsPointWithinInteractiveArea   C#
System.Windows.Documents.TextEditorMouse.OnQueryCursor  C#
System.Windows.RoutedEventArgs.InvokeHandler    C#
System.Windows.EventRoute.InvokeHandlersImpl    C#
System.Windows.UIElement.RaiseEventImpl C#
System.Windows.UIElement.RaiseTrustedEvent  C#
System.Windows.Input.InputManager.ProcessStagingArea    C#
System.Windows.Input.MouseDevice.UpdateCursorPrivate    C#
System.Windows.Input.MouseDevice.PostProcessInput   C#
System.Windows.Input.InputManager.RaiseProcessInputEventHandlers    C#
System.Windows.Input.InputManager.ProcessStagingArea    C#
System.Windows.Input.InputProviderSite.ReportInput  C#
System.Windows.Interop.HwndMouseInputProvider.ReportInput   C#
System.Windows.Interop.HwndMouseInputProvider.FilterMessage C#
System.Windows.Interop.HwndSource.InputFilterMessage    C#
MS.Win32.HwndWrapper.WndProc    C#
MS.Win32.HwndSubclass.DispatcherCallbackOperation   C#
System.Windows.Threading.ExceptionWrapper.InternalRealCall  C#
System.Windows.Threading.ExceptionWrapper.TryCatchWhen  C#
System.Windows.Threading.Dispatcher.LegacyInvokeImpl    C#
MS.Win32.HwndSubclass.SubclassWndProc   C#
[Native to Managed Transition]  
UserCallWinProcCheckWow Unknown
DispatchMessageWorker   Unknown
[Managed to Native Transition]  
System.Windows.Threading.Dispatcher.PushFrameImpl   C#
System.Windows.Threading.Dispatcher.PushFrame   C#
System.Windows.Threading.Dispatcher.Run C#
System.Windows.Application.RunDispatcher    C#
System.Windows.Application.RunInternal  C#
System.Windows.Application.Run  C#
MyCompany.App.Main  Unknown   
[Native to Managed Transition]  
coreclr_t::execute_assembly C++
run_app_for_context C++
run_app C++
corehost_main   C++
execute_app C++
`anonymous namespace'::read_config_and_execute  C++
fx_muxer_t::handle_exec_host_command    C++
fx_muxer_t::execute C++
hostfxr_main_startupinfo    C++
exe_start   C++
wmain   C++
[Inline Frame] invoke_main  C++
__scrt_common_main_seh  C++
BaseThreadInitThunk()  Unknown
RtlUserThreadStart()   Unknown

下面是我的视图(一个用户控件)的一个非常简略的版本,其中包含创建图像元素的 ItemsControl

<UserControl
    x:Class="Core.Views.LayerView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:cv="clr-namespace:Core.View"
    >
    <Canvas>
        <ItemsControl ItemsSource="MyItems">
            <ItemsControl.Resources>
                <DataTemplate x:Key="PathOutputTemplate" DataType="{x:Type ShapeVm}">
                    <Path Stroke="Yellow" StrokeThickness="6" Data={Binding Geometry} />
                </DataTemplate>
                <DataTemplate x:Key="ImageOutputTemplate" DataType={x:Type ImageOutputVm}>
                    <Image Source="{Binding ImageSource}"/>
                </DataTemplate>
            </ItemsControl.Resources>
            <ItemsControl.ItemTemplateSelector>
              <cv:OutputTemplateSelector
                PathTemplate="{StaticResource PathOutputTemplate}"
                ImageTemplate="{StaticResource ImageOutputTemplate}"
                />
            </ItemsControl.ItemTemplateSelector>
        </ItemsControl>
    </Canvas>
</UserControl>

标签: wpf.net-corefixeddocument

解决方案


我今天在创建新报告时遇到了这个问题,并想为将来遇到此问题的任何开发人员发布我的解决方法。这确实是一种解决方法,而不是解决潜在问题的方法,因为我没有时间深入挖掘。

解决方法包括在我的图像控件上添加一个具有透明背景的边框控件,基本上防止任何鼠标事件在下面触发:

<Grid>
    <My-content-that-must-not-be-moused-over/>
    <Border Background="Transparent"></Border>
<Grid>

推荐阅读