1

我试图听一个按钮单击工具栏上的按钮,当单击该按钮时,该按钮会切换drawer_panel的主抽屉,但没有太大成功。

代码如下所示 - 方法 void menu_icon_handler() 中的代码不正确。

    <!DOCTYPE html>
    <link rel="import" href="../../../packages/polymer/polymer.html">
    <link rel="import" href="../../../packages/core_elements/core_drawer_panel.html">
    <link rel="import" href="../../../packages/core_elements/core_icon_button.html">
    <link rel="import" href="../../../packages/core_elements/core_toolbar.html">
    <link rel="import" href="../../../packages/paper_elements/roboto.html">
    <link rel="import" href="../../../packages/core_elements/core_toolbar.html">
    <link rel="import" href="../../../packages/paper_elements/paper_icon_button.html">
    <link rel="import" href="./reg/registrant-view.html">
    <link rel="import" href="./reg/patient-view.html">
    <link rel="import" href="drawer-form.html">

    <polymer-element name="main-form">

      <template>
        <core-drawer-panel id="core_drawer_panel">
          <section id="drawer" drawer>
            <drawer-form on-new-patient-event='{{newPatientHandler}}'></drawer-form>
          </section>

          <section id="main" main>
            <core-toolbar id="core_toolbar">
              <paper-icon-button
                icon="menu"
                id='menu_icon'
                on-click='{{menu_icon_handler}}'></paper-icon-button>
              <div id="div" flex>Toolbar</div>
            </core-toolbar>

            <section id='new-patient'>

            </section>

          </section>
        </core-drawer-panel>

      </template>

      <script type="application/dart">

        import 'package:polymer/polymer.dart';
        import 'dart:html';

        import 'package:core_elements/core_drawer_panel.dart';

        @CustomTag( 'main-form' )
        class MainForm extends PolymerElement
        {
          CoreDrawerPanel drawerPanel;

          MainForm.created() : super.created();

          void toggleDrawer()
          {
            bool state = drawerPanel.narrow;
          }

          void menu_icon_handler()
          {
            bool state =  drawerPanel.narrow;

            print ( state );
          }

          @override
          void attached()
          {
             super.attached();

             drawerPanel = $['drawerPanel']  as CoreDrawerPanel;
          }
        }
      </script>
    </polymer-element>

单击工具栏按钮时,我得到以下堆栈跟踪

例外:空对象没有 getter 'narrow'。

NoSuchMethodError: method not found: 'narrow'
Receiver: null

我看到空值,查看文档“窄”是 CoreDrawerPanel 的一个属性

4

1 回答 1

1

你用

drawerPanel = $['drawerPanel']  as CoreDrawerPanel;

但是您的 HTML 不包含带有 id 的元素drawerPanel,但是您有一个带有 id 的元素core_drawer_panel

更改此行中的 id 应该可以解决问题

drawerPanel = $['core_drawer_panel']  as CoreDrawerPanel;
于 2014-08-05T09:33:12.493 回答