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

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

问题描述

我刚刚在Xamarin Studio中开始使用MonoMac,而且我遇到了最奇怪的问题:



我有一个带有NSButton和NSTextField的窗口它。到目前为止,我已经删除了按钮上的事件处理程序,所以它不会做任何事情,除了当我点击它时突出显示。按钮创建代码如下所示:

  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;

...然后它就像这样添加到窗口中:

  nsView.AddSubview(control.Handle as NSView); 

(因为在这部分代码中,control.Handle键入为对象,而nsView是在窗口的主视图)。



所有运行和工作正常。但是,如果我反复点击该按钮,最终窗口就会关闭。没有错误,也不例外,应用程序本身不退出;当我使用它们时,菜单继续响应并高兴地记录消息。但是窗口很简单 -



这是非常可重复的:它发生在21次点击之后。如果我添加更新NSTextField的事件处理程序(例如,hello.Caption =Foo;)),那么它将在19次点击之后发生。无论我点击快还是慢点都没关系它的点击次数总是相同的。请注意,项目中没有关闭窗口的代码,窗口甚至没有关闭框;我知道在退出应用程序之前没有合法的方法来关闭。



我在这里感到困惑,不知道如何进一步调试。 Xamarin是否有某种评估限制,在多次事件之后关闭您的窗口?是否是框架错误?任何见解将不胜感激。

解决方案

原来是与这一个,略有不同的幌子。



简而言之,我没有保留对NSWindow的引用对象,而是让它超出范围。因此,GUI窗口会持续一段时间,但最终(在一些数量的事件或其他代码创建幕后垃圾)之后,它被垃圾收集器注意到并被处理掉。然后窗口被拆下来。



一旦你想到它就完全合理,并且发生在Xamarin和MonoMac(只是稍微不同的时间)。



当然,简单的解决方案是保留对窗口的引用,直到你完成它。问题解决了!



(是的,我觉得有点懦弱,但希望这个问题会被未来的Mac C#开发人员找到,并保存他们一些悲伤。 p>

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

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);

(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.

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.

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 turned out to be the same issue as this one, in a slightly different guise.

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.

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!

(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天全站免登陆