Windows Phone: how to manage shake events?

66 views Asked by At

The accelerometer is activated (if I set ReadingChanged it works). Why the shaking event isn't handled?

namespace AppExample
{
 public sealed partial class MainPage : Page
 {
    private Accelerometer accel;

    public MainPage()
    {
        this.InitializeComponent();
        this.NavigationCacheMode = NavigationCacheMode.Required;

        accel = Accelerometer.GetDefault();
        //accel.ReadingChanged += accel_ReadingChanged;
        accel.Shaken += accel_Shaken;
    }

    void accel_Shaken(Accelerometer sender, AccelerometerShakenEventArgs args)
    {
        Debug.WriteLine("shaken");
    }
 }
}
3

There are 3 answers

0
godidier On BEST ANSWER

If you mind, there is helper librairy called ShakeGestures to handle shake gestures for windows phone 8. check this question

3
Christian Amado On

You can call the Dispatcher in order to show the result on the main thread.

namespace AppExample
{
    public sealed partial class MainPage : Page
    {
        Accelerometer accel;

        public MainPage()
        {
            this.InitializeComponent();

            accel = Accelerometer.GetDefault();
            accel.ReadingChanged += accel_ReadingChanged;
            accel.Shaken += accel_Shaken;
        }

        await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            LabelTest.Text = "Shaken!! " +  args.Reading.AccelerationX.ToString();
        });

        async private void accel_Shaken(object sender, AccelerometerShakenEventArgs e)
        {
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                _shakeCount++;
                ScenarioOutputText.Text = _shakeCount.ToString();
            });
        }
    }
}
1
Burak Kaan Köse On

If you're running Windows Phone 8 , Shaken event won't trigger and does not raise any errors according to MSDN page.

Otherwise it seems like a weird bug to me , I couldn't find any information about it.