首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我鼠标悬停在上面太快时,FixedPage会导致ArgumentNullException

当我鼠标悬停在上面太快时,FixedPage会导致ArgumentNullException
EN

Stack Overflow用户
提问于 2020-03-22 04:58:26
回答 1查看 61关注 0票数 1

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

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

请注意,只有当我的UserControl驻留在FixedPage中由DocumentViewer显示的FixedDocument中时,才会发生这种情况。当我在其他上下文中使用相同的控件时(远离FixedDocument等人),它可以正常工作。

有没有人知道关于这个“窗口”的任何事情,或者它的原因是什么?

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

代码语言:javascript
复制
    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);

下面是抛出

代码语言:javascript
复制
public static IEnumerable GetChildren(DependencyObject current)
{
    if (current == null)
    {
        throw new ArgumentNullException("current"); 
    }

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

代码语言:javascript
复制
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

下面是我的视图的一个非常简短的版本(一个UserControl),其中包含创建图像元素的ItemsControl

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-07 04:36:08

今天,我在创建一个新报告时遇到了这个问题,我想把我的解决方法发布给任何遇到这个问题的未来开发人员。这实际上是一种变通方法,而不是对潜在问题的修复,因为我没有时间深入挖掘。

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

代码语言:javascript
复制
<Grid>
    <My-content-that-must-not-be-moused-over/>
    <Border Background="Transparent"></Border>
<Grid>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60793214

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档