无法将焦点放在画布上

发布于 2024-08-14 17:44:45 字数 2113 浏览 2 评论 0原文

我正在 ActionScript 中创建一个画布,如下所示:

private var cvs_preview:Canvas = null;

            private function show_preview():void
            {                               
                this.cvs_preview = new Canvas();        
                this.cvs_preview.id = "cvs_preview_1";      
                this.cvs_preview.setStyle('backgroundColor', 0x000000);
                this.cvs_preview.setStyle('backgroundAlpha', 1);
                this.cvs_preview.setStyle('borderColor', 0x417FDD);
                this.cvs_preview.setStyle('cornerRadius', 10);
                this.cvs_preview.setStyle('borderStyle', 'solid');
                this.cvs_preview.setStyle('dropShadowEnabled', true);
                var pt:Point = image.localToGlobal(new Point(image.x, image.y));                
                this.cvs_preview.x = pt.x - 50;
                this.cvs_preview.y = pt.y - 50;
                this.cvs_preview.height = 200;
                this.cvs_preview.width = 250; 
                //this.cvs_preview.addEventListener(FlexEvent.CREATION_COMPLETE, get_focus_on_canvas);
                //this.cvs_preview.focusManager.setFocus(
                //this.cvs_preview.addEventListener(MouseEvent.CLICK, end_preview_on_focus_change);
                this.cvs_preview.addEventListener(FocusEvent.MOUSE_FOCUS_CHANGE, end_preview_on_focus_change);
Application.application.addChild(this.cvs_preview); //add as top-most visible container

                btn_mini_preview.enabled = false;

            }

因此,在焦点更改时,我想运行“end_preview_on_focus_change()”,

但这不起作用。

根据我的理解,我认为画布一开始就没有获得任何焦点。我试图在画布创建完成后使用 focusManager.setFocus 来执行此操作。但即使这样也给了我一个错误。

我在 Creation.Complete 上尝试的代码是:

private function get_focus_on_canvas(e:FlexEvent)
            {
                focusManager.setFocus(e.target);
                //Alert.show("testing img complete");
            }

这给了我一个错误“1118:将静态类型 Object 的值隐式强制为可能不相关的类型 mx.managers:IFocusManagerComponent。”

基本上我只想使用画布的焦点移出事件。

有人可以帮我解决这个问题吗... 我很长时间以来一直在关注这个问题。

问候 泽尚

I am creating a canvas in actionscript like :

private var cvs_preview:Canvas = null;

            private function show_preview():void
            {                               
                this.cvs_preview = new Canvas();        
                this.cvs_preview.id = "cvs_preview_1";      
                this.cvs_preview.setStyle('backgroundColor', 0x000000);
                this.cvs_preview.setStyle('backgroundAlpha', 1);
                this.cvs_preview.setStyle('borderColor', 0x417FDD);
                this.cvs_preview.setStyle('cornerRadius', 10);
                this.cvs_preview.setStyle('borderStyle', 'solid');
                this.cvs_preview.setStyle('dropShadowEnabled', true);
                var pt:Point = image.localToGlobal(new Point(image.x, image.y));                
                this.cvs_preview.x = pt.x - 50;
                this.cvs_preview.y = pt.y - 50;
                this.cvs_preview.height = 200;
                this.cvs_preview.width = 250; 
                //this.cvs_preview.addEventListener(FlexEvent.CREATION_COMPLETE, get_focus_on_canvas);
                //this.cvs_preview.focusManager.setFocus(
                //this.cvs_preview.addEventListener(MouseEvent.CLICK, end_preview_on_focus_change);
                this.cvs_preview.addEventListener(FocusEvent.MOUSE_FOCUS_CHANGE, end_preview_on_focus_change);
Application.application.addChild(this.cvs_preview); //add as top-most visible container

                btn_mini_preview.enabled = false;

            }

So on the focus change i want to run the "end_preview_on_focus_change()"

but this is not working.

As per my understanding, i think the canvas not getting any focus in the first place. I was trying to use focusManager.setFocus to do that after the canvas's creation complete. but even that is giving me an error.

the code i was trying on Creation.Complete is :

private function get_focus_on_canvas(e:FlexEvent)
            {
                focusManager.setFocus(e.target);
                //Alert.show("testing img complete");
            }

this is giving me an error "1118: Implicit coercion of a value with static type Object to a possibly unrelated type mx.managers:IFocusManagerComponent."

basically i just want to use the focus out event of the canvas.

Can someone help me with this...
I have been on this issue since a long time.

Regards
Zeeshan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

戏蝶舞 2024-08-21 17:44:45

错误是正确的。您有一个 Object 类型的对象,您尝试将其用作 IFocusManagerComponent。这是行不通的。要完成该行代码,您需要执行类似的操作

focusManager.setFocus( IFocusManagerComponent( e.target ) );

,当然,假设目标实现了 IFocusManagerComponent 。否则它会给你一个错误(在这种情况下很可能会发生,因为 Canvas 没有被列为 IFocusManagerComponent)。好消息是 Canvas 确实有一个 drawFocus 方法可以完成同样的事情。

至于您的 MOUSE_FOCUS_CHANGE 事件,只有当对象已经拥有焦点然后失去焦点时才会触发该事件。我认为你最好使用FlexEvent.CREATION_COMPLETE。这将确保组件已向 Flex SDK 中的所有相应类注册自身,以便 FocusManager 甚至可以知道新对象。无论您做什么,都不要尝试将焦点集中在尚未添加到舞台上的内容上(即:Event.ADDED 已被调用)。

作为另一条建议 - Event.ADDED 冒泡,请确保 event.currentTarget == event.target 以确保您正在侦听正确的对象。否则,您可能会错误地多次调用同一函数。

The error is correct. You have an object of type Object which you are trying to use as an IFocusManagerComponent. This will not work. To accomplish that line of code, you need to do something like

focusManager.setFocus( IFocusManagerComponent( e.target ) );

This, of course, assumes that the target implements IFocusManagerComponent. It will give you an error otherwise (and likely will in this case because Canvas is not listed as an IFocusManagerComponent). The good news is that Canvas does have a drawFocus method which will accomplish the same thing.

As to your MOUSE_FOCUS_CHANGE event, that will only be fired if an object already HAS focus and then loses it. I think you are better off using FlexEvent.CREATION_COMPLETE. This will ensure that the component has registered itself with all of the appropriate classes in the Flex SDK so that the FocusManager can even be aware of the new object. Whatever you do, do not try to set focus on something which has not been added to the stage (ie: Event.ADDED has been called).

As another piece of advice -- Event.ADDED bubbles, make sure that event.currentTarget == event.target to make sure that you are listening to the correct object. Otherwise, you might be calling the same function multiple times erroneously.

丑疤怪 2024-08-21 17:44:45

正如其他人提到的那样,只有少数类实现了 IFocusManagerComponent ,而 Canvas 不是其中之一。如果您确实必须调用 FocusManager.setFocus(),则必须扩展画布类来实现此接口并使用该类。您不必编写任何方法来实现此接口,所有方法都已由 UIComponent 本身实现。

//FocusableCanvas.as (include appropriate package and import statements)
public class FocusableCanvas extends Canvas implements IFocusManagerComponent
{
    public function FocusableCanvas()
    {
        super();
    }
}

//Now use this class instead of Canvas
this.cvs_preview = new FocusableCanvas();


//setFocus in creation complete handler
FocusManager.setFocus(IFocusManagerComponent(e.target));

但是,如果您只想在创建画布时将焦点设置在画布上,则可以请从 creationComplete 处理程序调用 canvas.setFocus()

private function get_focus_on_canvas(e:FlexEvent)
{
    Canvas(e.currentTarget).setFocus();
    trace("done");
}

Only a few classes implement IFocusManagerComponent as others mentioned and Canvas is not one of them. If you really must call FocusManager.setFocus() you will have to extend the canvas class to implement this interface and use that class instead. You don't have to write any methods to implement this interface, all methods have already been implemented by UIComponent itself

//FocusableCanvas.as (include appropriate package and import statements)
public class FocusableCanvas extends Canvas implements IFocusManagerComponent
{
    public function FocusableCanvas()
    {
        super();
    }
}

//Now use this class instead of Canvas
this.cvs_preview = new FocusableCanvas();


//setFocus in creation complete handler
FocusManager.setFocus(IFocusManagerComponent(e.target));

But if all you want to do is to set focus on the canvas upon it's creation, you can call canvas.setFocus() from the creationComplete handler instead.

private function get_focus_on_canvas(e:FlexEvent)
{
    Canvas(e.currentTarget).setFocus();
    trace("done");
}
淡墨 2024-08-21 17:44:45

我看到两个问题,并且没有完美的解决方案。运气好的话,这可以帮助你。

首先,e.target 返回一个具有 Object 类型的对象类型转换。这解释了您的隐式强制错误,因为 Object 没有实现 IFocusManagerComponent。

其次,iFocusManagerComponent 仅由 Accordion、AdvancedListBase、Button、ButtonBar、ChartBase、ComboBase、DateChooser、DateField、HTML、ListBase、MenuBar、NumericStepper、TabNavigator、TextArea、TextInput、UIMovieClip 实现,按照 Flex 3.4 AS3 参考中的此条目

这让我相信 Canvas 元素无法获得焦点,而只是通过继承 UIComponent 继承了对 FocusManager 的访问权限。

我能看到的唯一解决方案是利用 Canvas 以外的东西来处理与焦点相关的问题,或者子类化 Canvas 并实现 iFocusManagerComponent,尽管这看起来相当复杂。

编辑
对于上述解决方案中缺少 drawFocus 的情况,我们深表歉意。

I see two problems, and no perfect solutions. With any luck, this can help you out.

First of all, e.target returns an object typecast with type Object. This explains your implict coercion error, because Object does not implement IFocusManagerComponent.

Second, iFocusManagerComponent is only implemented by Accordion, AdvancedListBase, Button, ButtonBar, ChartBase, ComboBase, DateChooser, DateField, HTML, ListBase, MenuBar, NumericStepper, TabNavigator, TextArea, TextInput, UIMovieClip as per this entry in the Flex 3.4 AS3 Reference.

This leads me to believe that a Canvas element cannot take focus and has simply inherited access to the FocusManager through inheritance of UIComponent.

The only solutions I can see are to utilize something other than Canvas to handle your focus related concerns, or subclass Canvas and implement iFocusManagerComponent, though that looks fairly complex.

Edit
Apologies for missing drawFocus in the above solution.

北城半夏 2024-08-21 17:44:45

请尝试;

private function get_focus_on_canvas(e:FlexEvent)
{
    this.cvs_preview.setFocus();
}

Please try;

private function get_focus_on_canvas(e:FlexEvent)
{
    this.cvs_preview.setFocus();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文