繁体   English   中英

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

[英]Navigation between two screens Roku Developer - BrightScript

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

测试屏幕1.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
          

主.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", "$2b$10$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 go 到屏幕 2,反之亦然在两个屏幕之间导航回来?

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM