You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Roku开发:如何从主线程监听PosterGrid选中事件并切换屏幕

解决Roku主线程监听PosterGrid选中事件的方法

问题核心在于需要把PosterGrid的itemSelected事件传递到VideosScreen节点的自定义字段中,让主线程能通过监听该字段捕获事件。具体步骤如下:

1. 在VideosScreen的XML中添加自定义字段

在VideosScreen的根节点内添加一个可观察的字段,用来传递选中的索引或数据:

<screen>
  <!-- 原有的PosterGrid代码 -->
  <PosterGrid 
            id = "videosGrid" 
            basePosterSize = "[ 312, 188 ]" 
            caption1NumLines = "1" 
            numColumns = "3" 
            numRows = "5" 
            itemSpacing = "[ 20, 25 ]" 
            translation = "[ 100, 200]"
          />
  
  <!-- 添加自定义字段 -->
  <Field id="selectedPosterIndex" type="int" value="-1" />
</screen>

2. 在VideosScreen的线程中转发事件

修改VideosScreen里监听itemSelected的代码,当事件触发时更新自定义字段的值:

m.videosGrid = m.top.findNode("videosGrid")

' 监听PosterGrid的itemSelected事件
m.videosGrid.observeField("itemSelected", "onItemSelected")

sub onItemSelected(event as object)
    ' 获取选中条目的索引
    selectedIndex = event.getData().selectedIndex
    ' 更新VideosScreen的自定义字段
    m.top.selectedPosterIndex = selectedIndex
end sub

3. 修正主线程的监听代码

原代码中存在回调函数名不匹配的问题(ObserveField指定的是OnGridScreenItemSelected,但定义的是OnVideoGridScreenItemSelected),需要统一名称确保逻辑正确:

sub ShowVideoGridScreen()
    m.VideosGridScreen = CreateObject("roSGNode", "VideosScreen")
    ' 监听VideosScreen的自定义字段
    m.VideosGridScreen.ObserveField("selectedPosterIndex", "OnVideoGridScreenItemSelected")
    ShowScreen(m.VideosGridScreen)
end sub

sub OnVideoGridScreenItemSelected(event as object)
    print "------- Video Item Is Selected -----------"
    ' 可以通过event.getData()获取选中的索引值
    selectedIndex = event.getData()
    print "Selected index: "; selectedIndex
    ShowVideoScreen()
end sub

关键说明

  • 自定义字段的类型可按需调整:如果需要传递完整条目数据,可定义为assocarray类型。
  • 必须确保itemSelected事件触发时正确更新自定义字段,主线程才能捕获到字段变化。
  • 回调函数名必须和ObserveField中指定的名称完全一致,否则无法触发回调。

内容的提问来源于stack exchange,提问作者aleem aheer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 17:45:27