MonoMac窗口关闭没有错误 [英] MonoMac window closing without error

查看:214
本文介绍了MonoMac窗口关闭没有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始接触MonoMac在Xamarin Studio和我碰到的最奇怪的问题:

I'm just starting out with MonoMac in Xamarin Studio, and I've run into the strangest problem:

我与NSButton和的NSTextField上的窗口它。通过这一点,我切出按钮上的事件处理程序,所以当我点击它,它不会做任何事,除了亮点。按钮创建代码如下所示:

I a window with an NSButton and a NSTextField on it. By this point I've cut out the event handler on the button, so it doesn't DO anything, except highlight when I click it. The button creation code looks like this:

nsButton = new NSButton(new System.Drawing.RectangleF(0, 0, 100, 100));
nsButton.BezelStyle = NSBezelStyle.RoundRect;
nsButton.Font = NSFont.SystemFontOfSize(
    NSFont.SystemFontSizeForControlSize(NSControlSize.Regular));
nsButton.StringValue = text;



...然后将其添加到窗口,像这样:

...and then it gets added to the window like so:

nsView.AddSubview(control.Handle as NSView);



(因为这部分代码,control.Handle类型为对象,一个NSView是在窗口上主视图)。

(because in this part of the code, control.Handle is typed as object, and nsView is the main view on the window).

所有运行并在第一次正常工作。但是,如果我重复单击该按钮,最终只是窗口关闭。没有错误,也不例外,应用程序本身不会退出;菜单继续反应,当我使用他们兴致勃勃地记录消息。但窗口很简单 - 去

All runs and works fine at first. But, if I click repeatedly on that button, eventually the window just closes. No error, no exception, and the app itself doesn't quit; menus continue to respond and cheerfully log messages when I use them. But the window is simply -- gone.

这是非常重复:后21的点击情况。如果我添加的事件处理程序更新的NSTextField(例如hello.Caption =富),那么它经过19次点击情况。不要紧,我是否点击快或慢;它总是相同的点击次数。需要注意的是在项目没有代码以关闭窗口,该窗口甚至没有一个关闭框;我知道没有合法的方式来关闭它短暂退出应用程序。

It's extremely repeatable: it happens after 21 clicks. If I add an event handler that updates the NSTextField (e.g. hello.Caption="Foo";), then it happens after 19 clicks. It doesn't matter whether I click fast or slow; it's always the same number of clicks. Note that there is no code in the project to close the window, and the window doesn't even have a close box; I know of no legitimate way to close it short of quitting the app.

在这里,我百思不得其解,不知道如何继续调试。确实Xamarin有某种有这么一对多事件后关闭您的Windows评估限制?它是一个框架的bug? 。任何有识之士将不胜感激。

I am baffled here, and don't know how to debug this further. Does Xamarin have some sort of evaluation limit that closes your windows after so-many events? Is it a framework bug? Any insight will be greatly appreciated.

推荐答案

这竟然是相同的问题的this~~V 之一,在一个稍微不同的幌子。

This turned out to be the same issue as this one, in a slightly different guise.

在总之,我是不是要保持将被NSWindow参考对象,而是被让它走出去的范围。因此,GUI窗口会坚持一段时间了,但最终(经过一番一些事件或其他代码创建背后的幕后垃圾),它是发现和垃圾收集处理。然后,窗口被拆除。

In short, I wasn't keeping a reference to the NSWindow object, but instead was letting it go out of scope. So the GUI window would stick around for a while, but eventually (after some number of events or other code creating behind-the-scenes garbage) it is noticed and disposed of by the garbage collector. The window is then torn down.

这都是完全合理的,一旦你想起来,都Xamarin和MonoMac(只是时间略有不同)下发生的。

It's all perfectly reasonable once you think of it, and happens under both Xamarin and MonoMac (just at slightly different times).

最简单的解决方案,当然,是要保留到窗口的引用,直到你真正用它做。问题解决了!

The simple solution, of course, is to retain a reference to the window until you're truly done with it. Problem solved!

(是的,我觉得有点不好意思,但希望这个问题会被未来的Mac C#开发人员又找到,并保存一些悲痛。)

(And yes, I feel a bit sheepish, but hopefully this question will get found by future Mac C# developers, and save them some grief.)

这篇关于MonoMac窗口关闭没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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