创建一个仅在 roku 中单击“确定”时退出的对话框

问题描述 投票:0回答:1

如何在 roku 中创建一个对话框,该对话框在单击遥控器上的后退按钮时打开。当在对话框中选择“确定”选项时,应用程序退出。 这是main.brs中的代码

sub Main()
    ShowChannelRSGScreen()
end sub

sub ShowChannelRSGScreen()
    ' The roSGScreen object is a SceneGraph canvas that displays the contents of a Scene node instance
    screen = CreateObject("roSGScreen")
    ' message port is the place where events are sent
    m.port = CreateObject("roMessagePort")
    ' sets the message port which will be used for events from the screen
    screen.SetMessagePort(m.port)
    ' every screen object must have a Scene node, or a node that derives from the Scene node
    scene = screen.CreateScene("MainScene")
    screen.Show() ' Init method in MainScene.brs is invoked

    ' event loop
    while(true)
        ' waiting for events from screen
        msg = wait(0, m.port)
        msgType = type(msg)
        if msgType = "roSGScreenEvent"
            if msg.IsScreenClosed() then return
        end if
    end while
end sub

我尝试了上面的代码,但我不知道如何创建退出框对话框。

roku brightscript scenegraph
1个回答
0
投票

您可以利用 roku 的

StandardMessageDialog
来实现此目的。总结一下:

  • 创建一个新的StandardMessageDialog
  • 在其上设置一些道具,注册回调,然后显示它
  • 当用户点击按钮时,相应地处理逻辑

这是您的代码,已更新以显示其工作原理:

MainScene.brs

sub init()
end sub

function onKeyEvent(key as string, press as boolean) as boolean
  if press and key = "back" then
    'create the dialog
    dialog = createObject("roSGNode", "StandardMessageDialog")
    '.message is an array of messages
    dialog.message = ["Do you really want to exit?"]
    dialog.buttons = ["Yes", "No"]
    'register a callback function for when a user clicks a button
    dialog.observeFieldScoped("buttonSelected", "onDialogButtonClick")

    'assigning the dialog to m.top.dialog will "show" the dialog
    m.top.dialog = dialog
    return true
  end if
end function

function onDialogButtonClick(evt)
  buttonIndex = evt.getData()
  'did the user click "Yes"
  if buttonIndex = 0 then
    'set appExit which will kill the channel in the main loop
    m.top.appExit = true

  'user clicked "no"
  else
      'close the dialog
      m.top.dialog.close = true
      return true
  end if
end function

MainScene.xml

<?xml version="1.0" encoding="utf-8" ?>
<component name="MainScene" extends="Scene">
  <script type="text/brightscript" uri="MainScene.brs" />
  <interface>
    <field id="appExit" type="bool" alwaysnotify="true" value="false"/>
  </interface>
</component>

pkg:/source/main.brs

sub Main()
  ShowChannelRSGScreen()
end sub

sub ShowChannelRSGScreen()
  ' The roSGScreen object is a SceneGraph canvas that displays the contents of a Scene node instance
  screen = CreateObject("roSGScreen")
  ' message port is the place where events are sent
  m.port = CreateObject("roMessagePort")
  ' sets the message port which will be used for events from the screen
  screen.SetMessagePort(m.port)
  ' every screen object must have a Scene node, or a node that derives from the Scene node
  scene = screen.CreateScene("MainScene")
  screen.Show() ' Init method in MainScene.brs is invoked
  'observe the appExit event to know when to kill the channel
  scene.observeField("appExit", m.port)
  'focus the scene so it can respond to key events
  scene.setFocus(true)


  ' event loop
  while true
    msg = wait(0, m.port)
    msgType = type(msg)

    if msgType = "roSGScreenEvent" then
      if msg.isScreenClosed() then
        return
      end if
    'respond to the appExit field on MainScene
    else if msgType = "roSGNodeEvent" then
      field = msg.getField()
      'if the scene's appExit field was changed in any way, exit the channel
      if field = "appExit" then
        return
      end if
    end if
  end while
end sub
© www.soinside.com 2019 - 2024. All rights reserved.