在两个屏幕之间导航 Roku Developer - BrightScript

Navigation between two screens Roku Developer - BrightScript

我试图通过来回点击按钮在两个屏幕之间导航。我有以下代码。

TestScreen1.xml

<?xml version="1.0" encoding="utf-8" ?>
<component name="TestScreen1" extends="Scene"> 
    <children>
      <Label id="myLabelScreen1" 
        width="1280" 
        height="720" 
        horizAlign="center"
        vertAlign="top"
        translation="[0, 30]"
        />
        <Poster
            id="screenAPosterScreen1"
            translation="[470, 300]"
            width="0.0"
            height="0.0"
            visible="true">
        </Poster>
         <Button
            id="screen1Button"
            text="Go to Screen 2"
            width="100"
            height ="70"
            translation="[470, 450]"
            showFocusFootprint="true"
         />
 </children>
<!-- BrightScript Portion -->
<script type="text/brightscript" uri="TestScreen1.brs" />
<script type="text/brightscript" uri="pkg:/source/Main.brs" />
<!-- End of BrightScript Portion -->
</component>

TestScreen1.brs

function init()
     m.top.setFocus(true)
     m.myLabel = m.top.findNode("myLabelScreen1")
     m.screenAPoster = m.top.findNode("screenAPosterScreen1")
            
     m.Button = m.top.findNode("screen1Button")
     m.Button.observeField("buttonSelected", "onButton1Press")
     m.Button.setFocus(true)
        
     jsonData = ParseJson(m.global.mystuff)
        
     m.myLabel.text = jsonData.screens.a.title
     m.screenAPoster.uri = jsonData.screens.a.logo
        
     'Set the font size
     m.myLabel.font.size = 92
        
     'Set the color to light blue
      m.myLabel.color = "0x72D7EEFF"
end function
        

现在TestScreen2.xml

<?xml version="1.0" encoding="utf-8" ?>
<component name="TestScreen2" extends="Scene"> 
    <children>
      <Label id="myLabelScreen2" 
        width="1280" 
        height="720" 
        horizAlign="center"
        vertAlign="top"
        translation="[0, 30]"
        />
        <Poster
            id="screenAPosterScreen2"
            translation="[470, 300]"
            width="0.0"
            height="0.0"
            visible="true">
        </Poster>
        
         <Button
            id="screen2Button"
            text="Go to Screen 1"
            width="100"
            height ="70"
            translation="[470, 450]"
            showFocusFootprint="true"
         />
 </children>
<!-- BrightScript Portion -->
<script type="text/brightscript" uri="TestScreen2.brs" />
<script type="text/brightscript" uri="pkg:/source/Main.brs" />
<!-- End of BrightScript Portion -->
</component>

TestScreen2.brs

function init()
      m.top.setFocus(true)
      m.top.backgroundColor = "#FF0000"
      m.myLabel = m.top.findNode("myLabelScreen2")
      m.screenAPoster = m.top.findNode("screenAPosterScreen2")
            
      m.Button = m.top.findNode("screen2Button")
      m.Button.observeField("buttonSelected", "onButton2Press")
      m.Button.setFocus(true)
            
      jsonData = ParseJson(m.global.mystuff)
            
      m.myLabel.text = jsonData.screens.b.title
      m.screenAPoster.uri = jsonData.screens.b.logo
            
      'Set the font size
       m.myLabel.font.size = 92
            
       'Set the color to light blue
       m.myLabel.color = "0x72D7EEFF"
end function
          

Main.brs 文件

sub Main()
    print "in showChannelSGScreen"
    'Indicate this is a Roku SceneGraph application'
    screen = CreateObject("roSGScreen")
    m.port = CreateObject("roMessagePort")
    screen.setMessagePort(m.port)

    scene = screen.CreateScene("TestScreen1")

    m.global = screen.getGlobalNode()
    m.global.AddField("mystuff", "string", false)

    port = CreateObject("roMessagePort")
    request = CreateObject("roUrlTransfer")

    request.AddHeader("secret-key", "b$uFTmoV/NUudBt3K/t8h9H.c08SIwq29I9RiZskcr5k.tU8lvpwfJ2")
    request.AddHeader("Accept", "application/json")
    request.AddHeader("Content-Type", "application/json")
    request.EnablePeerVerification(false)
    request.EnableHostVerification(false)
    request.RetainBodyOnError(true)
    request.SetCertificatesFile("common:/certs/ca-bundle.crt")
    request.InitClientCertificates()
    request.SetMessagePort(port)

    request.SetURL("https://api.jsonbin.io/b/5e7e4017862c46101abf301f")

    response = ParseJson(request.GetToString())

    m.global.mystuff = request.GetToString()

    screen.show()

    while(true)
        msg = wait(0, m.port)
        msgType = type(msg)
        if msgType = "roSGScreenEvent"
            if msg.isScreenClosed() then return
        end if
    end while
end sub

sub onButton1Press(event as object)
    print "go to screen2"
    'how to navigate to screen2
end sub

sub onButton2Press(event as object)
     print "go to screen2"
    'how to navigate to screen1
end sub

如何在两个屏幕之间来回导航,单击 screen1Button 转到屏幕 2,反之亦然在两个屏幕之间导航回来?

Roku 应用程序中只能有一个“场景”。更改您的 test2 xml 以扩展“组”。 还要避免在 Main 中调用 API,创建一个单独的 Task 节点来执行此操作。 同样对于 m.Button.observeField("buttonSelected", "onButton2Press"),在同一个 brs 文件中定义 sub,因为范围很重要。

最好从入门文档开始,以掌握正确的基础知识。

您可以使用 SGDEX 中实现的 MultiStack 功能来保存每个屏幕的状态并交换它们。