导致此脚本中出现“未绑定上下文”错误的原因是什么?

时间:2014-04-13 06:25:39

标签: rebol rebol3 r3-gui

我有以下R3-GUI脚本:

Rebol [
    file: %context.reb
]

either exists? %r3-gui.r3 [do %r3-gui.r3][load-gui]

view [
    title "First Window"
    hgroup [
        lab: label "fld 1 "
        fld1: field "Field Data 1"
    ]
    button "Display fld 1" on-action [if error? try [probe get-face fld1][alert "Can't read fld1"]]
    button "Display fld 2" on-action [if error? try [probe get-face fld2][alert "Can't read fld2"]]
    button "Open 2nd Window" on-action [
        view [
            title "Second Window"
            hgroup [
                label "fld 2" fld2: field "field 2 data"
            ]
            button "Display fld1" on-action [if error? err: try [probe get-face fld1][probe err alert "Can't read fld1"]]
            button "Display fld2" on-action [if error? err: try [probe get-face fld2][probe err alert "Can't read fld2" ]]
        ]
    ]
]

当我点击第二个窗口中的“显示fld2”按钮访问fld2的内容​​时,我收到** Script error: fld2 word is not bound to a context错误。这是什么原因?如何访问第二个窗口中的fld2字?

2 个答案:

答案 0 :(得分:1)

因为fld2是匿名函数的本地函数而不是绑定到用户上下文

>> help win-face/facets/tab-face/actors 
WIN-FACE/FACETS/TAB-FACE/ACTORS is a block of value: [on-action make function! [[face arg
        /local fld2 err
    ][
        view layout [
            title "Second Window"
            hgroup [
                label "fld 2" fld2: field "field 2 data"
            ]
            button "Display fld 1" on-action [if error? try [probe get-face fld1] [alert "Can't read fld1"]]
            button "Display fld2" on-action [if error? err: try [probe get-face fld2] [probe err alert "Can't read fld2"]]
        ]
    ]]]
>>

如果你这样做的话,它会起作用

 l2: layout [
    title "Second Window"
    hgroup [
       label "fld 2" fld2: field "field 2 data"
    ]
    button "Display fld1" on-action [if error? err: try [probe get-face fld1][probe err alert "Can't read fld1"]]
    button "Display fld2" on-action [if error? err: try [probe get-face fld2][probe err alert "Can't read fld2" ]]
]


view  l1: layout [
   title "First Window"
   hgroup [
       lab: label "fld 1 "
       fld1: field "Field Data 1"
   ]
   button "Display fld 1" on-action [if error? try [probe get-face fld1][alert "Can't read fld1"]]
   button "Display fld 2" on-action [if error? try [probe get-face fld2][alert "Can't read fld2"]]
   button "Open 2nd Window" on-action [
      view l2
   ]
]

答案 1 :(得分:1)

问题出现了,因为parse-layout函数创建的匿名函数应该是一个闭包,而不是。请参阅解决问题的https://gist.github.com/earl/a009454787d9fe4cfaca差异。