Visual Studio调试出现奇怪的错误

时间:2021-11-21 02:52:04

I'm developing an application in WPF. I've created a list box which has two events i.e MouseDoubleClick & MouseRightButtonUp. When I run the program and right-click on any item, the event is triggered but it does not show the value of any variable in debugging mode. However, If I double-click on any item in the list box, I can see the variables values defined in that event in debug-time. I'm not sure why the debugging works fine in one event and doesn't work same in another event.

我正在用WPF开发一个应用程序。我创建了一个列表框,其中包含两个事件,即MouseDoubleClick和MouseRightButtonUp。当我运行程序并右键单击任何项​​目时,事件被触发但在调试模式下它不显示任何变量的值。但是,如果我双击列表框中的任何项目,我可以在调试时看到该事件中定义的变量值。我不确定为什么调试在一个事件中工作正常,在另一个事件中不能正常工作。


MouseRightButtonUp event on debugging Visual Studio调试出现奇怪的错误

调试时MouseRightButtonUp事件


MouseDoubleClick event on debugging Visual Studio调试出现奇怪的错误

调试时的MouseDoubleClick事件


here's the xaml code:

这是xaml代码:

<ListBox ItemsSource="{Binding Items}" Name="detailList" Margin="5,5,0,0"
                 ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                 MouseDoubleClick="detailList_MouseDoubleClick" MouseRightButtonUp="DetailList_OnMouseRightButtonUp">

                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel Orientation="Horizontal"></WrapPanel>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>

                <ListBox.ItemTemplate >
                    <DataTemplate>
                        <StackPanel Orientation="Vertical" Width="90" >
                            <Image Width="80" Source="{Binding Image}"/>
                            <TextBlock  Width="60" Height="30" TextWrapping="Wrap" FontSize="11" Text="{Binding Name}" TextAlignment="Center"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

And, here's the code behind of MouseRightButtonUp & MouseDoubleClick events:

而且,这是MouseRightButtonUp和MouseDoubleClick事件背后的代码:

 private void DetailList_OnMouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            //get the reference of current treeview
            var items = (ListBox)sender;
            //hold the reference of selected node

            string fullPath = null;

            // If selected item is a drive
            if (items.SelectedItem is DirectoryItemViewModel)
            {
                fullPath = OnDriveDoubleClick((items.SelectedItem as DirectoryItemViewModel));
            }
            // If selected item is a folder
            else if (items.SelectedItem is DirectoryItem)
            {
                fullPath = OnFolderDoubleClick((items.SelectedItem as DirectoryItem));
            }

            bool isFile = fullPath.Contains(".");

            if (isFile)
            {
                this.m_Handler.FamilyPath = fullPath;
                //On single click
                this.m_Handler.SingleClicked = true;
                m_ExEvent.Raise();
                return;
            }

        }

private void detailList_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            //get the reference of current treeview
            var items = (ListBox)sender;
            //hold the reference of selected node

            string fullPath = null;

            // If selected item is a drive
            if (items.SelectedItem is DirectoryItemViewModel)
            {
                fullPath = OnDriveDoubleClick((items.SelectedItem as DirectoryItemViewModel));
            }
            // If selected item is a folder
            else if (items.SelectedItem is DirectoryItem)
            {
                fullPath = OnFolderDoubleClick((items.SelectedItem as DirectoryItem));
            }

            bool isFile = fullPath.Contains(".");

            if (isFile)
            {
                this.m_Handler.FamilyPath = fullPath;
                //On single click
              //  this.m_Handler.SingleClicked = true;
                m_ExEvent.Raise();
                return;
            }


            // Refresh the binding and view with the updated data in listbox
            List<DirectoryItem> dir = DirectoryStructure.GetDirectoryContent(fullPath);
            // Empty the old data from listbox
            detailList.DataContext = null;
            // bind new data to listbox
            detailList.ItemsSource = dir;
        }

Appreciate if anyone can help me identify the cause of this error. Thank you

感谢是否有人可以帮我确定此错误的原因。谢谢

3 个解决方案

#1


0  

Try to restart VS after changing mode to "Debug". I did not implement this but I found something here.

将模式更改为“Debug”后尝试重新启动VS.我没有实现这个,但我在这里找到了一些东西。

#2


0  

Have you tried to forcefully evaluate an expression? (select the expression and press Shift-F9)

你有没有试过强有力地评估表达? (选择表达式并按Shift-F9)

Usually, if you try to evaluate something that VS for some reason can't, it will at least give you a proper error message to see the root cause of the issue.

通常,如果您尝试评估VS由于某种原因无法做到的事情,它至少会给您一个正确的错误消息,以查看问题的根本原因。

I would bet that it may have something to do with code optimization. Make sure you run your application in Debug preset. Go to the project properties, and make sure you are generating full debug information and you don't have the "Optimize code" checkbox active.

我敢打赌它可能与代码优化有关。确保在Debug预设中运行应用程序。转到项目属性,并确保生成完整的调试信息,并且没有激活“优化代码”复选框。

#3


0  

I have able to resolve this by updating the VS 2017.

我可以通过更新VS 2017来解决这个问题。

#1


0  

Try to restart VS after changing mode to "Debug". I did not implement this but I found something here.

将模式更改为“Debug”后尝试重新启动VS.我没有实现这个,但我在这里找到了一些东西。

#2


0  

Have you tried to forcefully evaluate an expression? (select the expression and press Shift-F9)

你有没有试过强有力地评估表达? (选择表达式并按Shift-F9)

Usually, if you try to evaluate something that VS for some reason can't, it will at least give you a proper error message to see the root cause of the issue.

通常,如果您尝试评估VS由于某种原因无法做到的事情,它至少会给您一个正确的错误消息,以查看问题的根本原因。

I would bet that it may have something to do with code optimization. Make sure you run your application in Debug preset. Go to the project properties, and make sure you are generating full debug information and you don't have the "Optimize code" checkbox active.

我敢打赌它可能与代码优化有关。确保在Debug预设中运行应用程序。转到项目属性,并确保生成完整的调试信息,并且没有激活“优化代码”复选框。

#3


0  

I have able to resolve this by updating the VS 2017.

我可以通过更新VS 2017来解决这个问题。