Flex 文本区域控件未正确更新
我正在编写一个 Flex 应用程序,其中涉及非常频繁地修改文本区域。我遇到了文本区域有时不显示我的修改的问题。
下面的actionscript代码说明了我的问题:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600">
<mx:TextArea x="82" y="36" width="354" height="291" id="textArea" creationComplete="initApp()"/>
<mx:Script>
<![CDATA[
private var testSentence:String = "The big brown fox jumps over the lazy dog.";
private var testCounter:int = 0;
private function initApp():void {
var timer:Timer = new Timer(10);
timer.addEventListener(TimerEvent.TIMER, playSentence);
timer.start();
}
private function playSentence(event:TimerEvent):void {
textArea.editable = false;
if (testCounter == testSentence.length) {
testCounter = 0;
textArea.text += "\n";
}
else {
textArea.text += testSentence.charAt(testCounter++);
}
textArea.editable = true;
}
]]>
</mx:Script>
</mx:Application>
当你在flex项目中运行上面的代码时,它应该一个字符一个字符地重复打印句子“The bigbrownfoxskisoverthelazydog.”。但是,如果您同时在文本区域中输入内容,您会注意到计时器打印的文本是扭曲的。
我真的很好奇为什么会发生这种情况。 Flex 的单线程特性以及在我进行修改时禁用文本区域的用户输入应该可以防止这种情况发生,但由于某种原因,这似乎不起作用。
我还必须注意,当以较大的间隔(大约 100 毫秒)运行计时器时,它似乎工作得很好,所以我很想认为这是 Flex 框架内部的某种同步问题。
关于可能导致问题的原因有什么想法吗?
I am writing a flex application that involves modifying a textarea very frequently. I have encountered issues with the textarea sometimes not displaying my modifications.
The following actionscript code illustrates my problem:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600">
<mx:TextArea x="82" y="36" width="354" height="291" id="textArea" creationComplete="initApp()"/>
<mx:Script>
<![CDATA[
private var testSentence:String = "The big brown fox jumps over the lazy dog.";
private var testCounter:int = 0;
private function initApp():void {
var timer:Timer = new Timer(10);
timer.addEventListener(TimerEvent.TIMER, playSentence);
timer.start();
}
private function playSentence(event:TimerEvent):void {
textArea.editable = false;
if (testCounter == testSentence.length) {
testCounter = 0;
textArea.text += "\n";
}
else {
textArea.text += testSentence.charAt(testCounter++);
}
textArea.editable = true;
}
]]>
</mx:Script>
</mx:Application>
When you run the above code in a flex project, it should repeatedly print, character by character, the sentence "The big brown fox jumps over the lazy dog.". But, if you are typing into the textarea at the same time, you will notice the text the timer prints is distorted.
I am really curious as to why this happens. The single-threaded nature of flex and disabling user input for the textarea when I make modifications should prevent this from happening, but for some reason this doesn't seem to be working.
I must note too that, when running the timer at larger intervals (around 100ms) it seems to work perfectly, so I am tempted to think it's some kind of synchronization issue in the internals of the flex framework.
Any ideas on what could be causing the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的儿子在出牙时遇到了困难,因此我醒了。而且,我不太确定我是如何偶然发现这个随机问题/答案的,因为我很少出现在 stackoverflow 上……但是……
你不应该因为文本区域而将帧速率提高 4 倍!我们谈论的是系统资源,更不用说您可以利用的非常优雅的 Flex 框架:
这是一个非常快速且符合 Flex-SDK 且令人满意的修复:
创建一个扩展 TextArea 的组件并添加以下属性并覆盖(如果这实际上是您想要做的):
然后更改您的 playSentence 来执行此操作:
这是对半常见问题的一个修复,应该允许您不将帧速率增加 4 倍以使您的应用程序正常运行并且你会更好地使用flex sdk。
我的2p。
祝你过得好,
杰里米
My son is having a rough go at this teething thing, hense my being awake. AND, I'm not really sure how I happened upon this random question/answer since I'm so rarely on stackoverflow.... but...
You SHOULD NOT have to increase your framerate by 4x because of a textArea! We're talking about system resources, not to mention a very elegant flex framework that you could instead leverage:
Here's a very quick, and flex-sdk compliant and happy fix:
create a component that extends TextArea and add the following property and override (if this in fact what you're trying to do):
Then change your playSentence to do this:
This is one fix to a semi-common problem that should allow you to not increase your frameRate by 4x to get your app to function correctly AND you'll be better working with the flex sdk.
My 2p.
Have a good one,
Jeremy
您面临的问题似乎是当您输入文本的同时调用添加股票代码功能时,通过手动输入文本,似乎会丢失文本的添加。虽然我不知道为什么。
我发现的解决方案似乎是确保框架脚本和计时器不满足(为了更快的文本输入,增加帧速率)10ms计时器100fps似乎工作没有任何问题(添加
frameRate =“100” 到
mx:Application
行)。the problem you are facing seems to be when the addition ticker function is called at the same time that you are entering text, by entering text manually it seems that it is then missing the addition of the text. though im not sure why.
the solution i found seems to be to make sure that the framescript and timer dont meet (for the faster text input, increase the framerate) for 10ms timer 100fps seems to work without any problems (add
frameRate="100"
tomx:Application
line).