roku/brightscript 开发的新手:是否可以将对象添加到全局关联数组(所有组件均可访问),该对象具有定义为属性之一的方法,并调用该方法?
主要.brs:
function Main()
init()
end function
function init()
screen = createObject("roSGScreen")
m.port = createObject("roMessagePort")
screen.SetMessagePort(m.port)
scene = screen.CreateScene("MainController")
screen.show()
o = {
getName: function() as string
return "John"
end function
}
setUpGlobal(screen)
m.global.addFields({mainMethods: o})
while(true)
msg = wait(0, m.port)
msgType = type(msg)
if msgType = "roSGScreenEvent"
if msg.isScreenClosed() then exit while
end if
end while
end function
function setUpGlobal(p_screen as Object)
m.global = p_screen.getGlobalNode()
m.global.id = "GlobalNode"
end function
.. 然后在另一个 MainController 中,在运行任务并返回数据之后......
主控制器.brs
function init()
loadConfig()
end function
function loadConfig()
m.config = createObject("roSGNode", "Configurator")
m.config.observeField("done", "onConfigLoaded")
m.config.observeField("fail", "onConfigError")
end function
function onConfigLoaded()
print "config loaded: " + m.global.mainMethods.getName()
end function
function onConfigError()
print "config failed to loaded"
end function
当它到达 MainController 的第 16 行时,我得到了这个:
在 BrightScript 组件或接口中找不到成员函数。(运行时错误 &hf4) 在 pkg:/components/MainController.brs(16)
这只是对可以/不能做什么的一般测试,因此请不要评论这是否是“良好做法”。我只想知道这是否可能,如果可能,我在这里错过了什么?谢谢你的帮助