为什么 Xamarin.Mac 窗口突然消失? [英] Why does Xamarin.Mac window abruptly disappear?

查看:90
本文介绍了为什么 Xamarin.Mac 窗口突然消失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发表了关于 MonoMac 中的一个错误,其中单击按钮 20 次左右后窗口会突然消失.事实证明,该错误似乎不会影响 Xamarin.Mac,因此我将我的项目切换到该错误.

I recently posted about a bug in MonoMac in which the window would abruptly disappear after clicking on a button 20 times or so. That bug, it turned out, doesn't seem to affect Xamarin.Mac, so I switched my project to that.

但现在我在不同的上下文中看到几乎相同的错误:在 NSTextField 中输入几行文本后,窗口以完全相同的方式消失.没有错误,没有例外;它就消失了.噗,没了!

But now I'm seeing virtually the same bug in a different context: after typing a few lines of text into an NSTextField, the window disappears in exactly the same manner. No error, no exception; it just vanishes. Poof, gone!

我在一个简单的项目中复制了这个:您可以通过创建一个新的空 Xamarin.Mac 项目,添加 这段代码 在一个新文件中,并运行.然后将键盘捣碎一会儿.5 或 6 行后,窗口消失.

I've reproduced this in a trivial project: you can see it yourself by creating a new empty Xamarin.Mac project, adding this code in a new file, and running. Then mash the keyboard a little while. After 5 or 6 lines, the window disappears.

(请注意,当我选择菜单命令时,应用菜单会继续工作,写入日志消息并执行我的代码.因此它看起来不像应用本身已经崩溃.)

(Note that the app menus continue to work, writing log messages and executing my code when I select the menu commands. So it doesn't appear as though the app itself has crashed.)

奇怪的是,这似乎只发生在该字段位于已覆盖 IsFlipped 的 NSView 子类中时.但是,IsFlipped 返回 true 还是 false 并不重要——如果该方法存在,那么错误就会发生;如果注释掉,错误似乎不会发生.

Curiously, this seems to only occur when the field is inside an NSView subclass where IsFlipped has been overridden. But it doesn't matter whether IsFlipped returns true or false -- if the method is there at all, the bug occurs; if commented out, the bug does not seem to occur.

那么:有没有其他人在 Xamarin.Mac 中遇到过这种错误?也许在其他情况下,这会给我们更多关于根本原因的线索?Xamarin 新手的任何调试技巧?

So: Has anyone else run into this sort of bug in Xamarin.Mac? Perhaps in other contexts that will give us more clues as to a root cause? Any debugging tips for a Xamarin newbie?

(我知道在某些项目中我可以通过避免 IsFlipped 来解决这个问题,但我担心它会以其他方式突然出现来咬我.)

(I know I could work around it in some projects by avoiding IsFlipped, but I worry that it would just pop up to bite me in some other way.)

推荐答案

好的,事实证明这根本不是 Xamarin.Mac 中的错误,而是我的代码中的错误.因为我没有保留对 NSWindow 对象的引用,它超出了范围,一旦垃圾收集器注意到它,它就会被处理掉并且 GUI 窗口被拆除.

OK, it turns out that this isn't a bug in Xamarin.Mac at all, but in my code. Because I wasn't retaining a reference to the NSWindow object, it went out of scope, and as soon as the garbage collector gets around to noticing it, it's disposed and the GUI window is torn down.

我们所要做的就是保留对窗口的引用(例如,在 AppDelegate 类中),问题就迎刃而解了.

All we have to do is retain a reference to the window (for example, in the AppDelegate class), and the problem goes away.

向 Xamarin 的 Chris Hamons 致敬,他立即跳到这里并迅速为我找到了问题.

Hats off to Chris Hamons at Xamarin, who jumped right on this and quickly found the problem for me.

这篇关于为什么 Xamarin.Mac 窗口突然消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆