简体   繁体   English

为什么此代码适用于Windows 7,但不适用于Windows XP?

[英]Why does this code work on Windows 7, but doesn't on Windows XP?

A little background: I'm a WPF to WinForms convertee and for some time I've been migrating my application. 一点背景:我是WPF到WinForms转换器,有一段时间我一直在迁移我的应用程序。

I was reported by a friend that my code doesn't work on Windows XP (it generates a stack overflow at startup) even though it works fine on Windows 7 (which I develop in). 我的朋友报告说我的代码在Windows XP上不起作用(它在启动时会产生堆栈溢出),即使它在Windows 7(我开发的)上工作正常。

After a little research, what caused the problem was something along these lines: 经过一番研究后,导致问题的原因是:

 private void listView1_SelectedIndexChanged(object sender, EventArgs e)
 {
     listView1.SelectedIndices.Clear();
     listView1.Items[0].Selected = true;
 }

Now that I noticed the obviously poor decision, I wasn't wondering why it doesn't work on Windows XP. 现在我注意到了明显不好的决定,我不知道为什么它在Windows XP上不起作用。 I was wondering why does it work on Windows 7 . 我想知道它为什么在Windows 7上运行

Obviously at some point the compiler figures out what I'm trying to do and prevents the same event to be fired over and over again, but I'd much rather have it do nothing, so I can see and squish the bug on sight on the platform I'm developing in, rather than have to test it under two platforms simultaneously. 很明显,在某些时候编译器会弄清楚我正在尝试做什么,并防止同一事件一次又一次地被触发,但我宁愿让它什么都不做,所以我可以看到并挤压看到的bug。我正在开发的平台,而不是必须同时在两个平台下测试它。 Back in WPF I could handle such behaviour manually by setting e.Handled to 'true', in WinForms apparently there's no such thing. 回到WPF我可以通过将e.Handled设置为'true'来手动处理这种行为,在WinForms中显然没有这样的事情。

Is there some sort of a compiler flag for this? 是否有某种编译器标志?

Try this: 尝试这个:

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
   if (!listView1.Items[0].Selected) {
       listView1.SelectedIndices.Clear();
       listView1.Items[0].Selected = true;
   }
}

You only want to SET selection ONCE, on your first item. 您只想在第一个项目上设置选择ONCE。 The problem is it's likely getting into a perpetual loop. 问题是它可能会进入永久循环。

As to why Windows 7 is more forgiving than XP I couldn't say. 至于为什么Windows 7比XP更宽容,我不能说。 Could be the order the LVM_* messages are processed in or anything. 可以是处理LVM_ *消息的顺序或任何内容。

Check and see if the .NET version makes any difference. 检查并查看.NET版本是否有任何区别。 If you have a newer version of .NET on your Windows 7 machine than on XP (very likely), then it is possible for there to be differences even if you are targeting the earlier version. 如果您的Windows 7计算机上的.NET版本比XP上的版本(很可能),那么即使您的目标是早期版本,也可能存在差异。

See what MSDN says about .NET backwards compatibility . 了解MSDN对.NET向后兼容性的看法。

this may work (NOT TESTED) 这可能有效(未测试)

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
   if(Environment.OSVersion.Version.Major < 6) listview1.SelectedIndexChanged -= new EventHandler(listView1_SelectedIndexChanged);
   listView1.SelectedIndices.Clear();
   listView1.Items[0].Selected = true;
   if(Environment.OSVersion.Version.Major < 6) listview1.SelectedIndexChanged += new EventHandler(listView1_SelectedIndexChanged);
}

edit look its OS specific :o 编辑看看它的特定操作系统:o

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM