locked
SimpleOrientationSensor orientationchanged never called

    Question

  • This is my code 

    if (orientationSensor = Windows.Devices.Sensors.SimpleOrientationSensor.getDefault()) {
          orientationSensor.addEventListener("orientationchanged", function(_arg) {
            var orientation;
            orientation = _arg.orientation;
            if (orientation === Windows.Devices.Sensors.SimpleOrientation.facedown) {
              return screenLock.lock();
            }
          });
        }
    but the event handler is never called.

    Friday, July 4, 2014 2:58 AM

Answers

  • Yes, the sample works. That's what freaks me out. Seems to be a kind of global reference problem or something. That the var goes out of scope and the JS VM thinks it can clean up the event handlers or so.

    edit: yes, is a global var ref problem. When I write it like this:

    if (window.orientationSensor = Windows.Devices.Sensors.SimpleOrientationSensor.getDefault()) {
          window.orientationSensor.addEventListener("orientationchanged", function(_arg) {
            var orientation;
            orientation = _arg.orientation;
            if (orientation === Windows.Devices.Sensors.SimpleOrientation.facedown) {
              return screenLock.lock();
            }
          });
        }

    then it works.

    Monday, July 7, 2014 9:41 AM

All replies

  • Hi pkursawe,

    I read the documentation: OrientationSensor class

    There is no "orentationchanged" event in the list, except ReadingChanged event.

    I believe that's the reason why your event handler is never called.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, July 7, 2014 5:46 AM
    Moderator
  • Yes there is http://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.sensors.simpleorientationsensor.aspx
    Monday, July 7, 2014 8:28 AM
  • Sorry, mistake, read the wrong documentation.

    If you run this sample (http://code.msdn.microsoft.com/windowsapps/SimpleOrientationSensor-d948ac62) on your device, can it be correctly called?

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, July 7, 2014 8:46 AM
    Moderator
  • Yes, the sample works. That's what freaks me out. Seems to be a kind of global reference problem or something. That the var goes out of scope and the JS VM thinks it can clean up the event handlers or so.

    edit: yes, is a global var ref problem. When I write it like this:

    if (window.orientationSensor = Windows.Devices.Sensors.SimpleOrientationSensor.getDefault()) {
          window.orientationSensor.addEventListener("orientationchanged", function(_arg) {
            var orientation;
            orientation = _arg.orientation;
            if (orientation === Windows.Devices.Sensors.SimpleOrientation.facedown) {
              return screenLock.lock();
            }
          });
        }

    then it works.

    Monday, July 7, 2014 9:41 AM
  • Question: is there another way to make the eventhandler stay without polluting the window namespace?
    Monday, July 7, 2014 11:02 AM