0

i've a problem with windows phone shakegesture library. I build an application which its shaking the sound will go out and its work nicely but strange bug make me confused. I've two page of it. This is my seperated code :

void Instance_ShakeGesture1(object sender, ShakeGestureEventArgs e)
    {            
        Stream stream = TitleContainer.OpenStream("Sounds/C.wav");
        effect = SoundEffect.FromStream(stream);
        effectInstance = effect.CreateInstance();
        if (effectInstance.State != SoundState.Playing || effectInstance == null)
        {
            FrameworkDispatcher.Update();
            effectInstance.Play();
        }

        else if (effectInstance.State == SoundState.Playing || effectInstance != null)
        {
            effectInstance.Stop();
        }
    }

    void Instance_ShakeGesture2(object sender, ShakeGestureEventArgs e)
    {
        Stream stream = TitleContainer.OpenStream("Sounds/D.wav");
        effect = SoundEffect.FromStream(stream);
        effectInstance = effect.CreateInstance();
        FrameworkDispatcher.Update();
        if (effectInstance.State == SoundState.Stopped || effectInstance == null)
        {
            effectInstance.Play();
        }

        else if (effectInstance.State == SoundState.Playing || effectInstance != null)
        {
            effectInstance.Stop();
        }
    }

Instance_ShakeGesture1 is my procedure to play a music when its shaking in Page 1 and Instance_ShakeGesture2 in Page 2. Strange bug was come when its shaking, if i shake page 1 Instance_ShakeGesture1 will executed after that I try move to page 2 and i shake it will execute Instance_ShakeGesture1 first and than Instance_ShakeGesture2. The Problem was come same when i try to shake Page 2 first and than Page 1, Instance_ShakeGesture2 will execute first and Instance_ShakeGesture2 in the second. I know this bug when i use breakpoint. Anyone know how to solve this problem? Thanks before :)

pacifistazero
  • 87
  • 2
  • 6

3 Answers3

1

Possibly the event Instance_ShakeGesture1 is still active when you navigate to the second page. try

Instance.ShakeEvent -= new EventHandler(Instance_ShakeGesture1);

inside the Instance_ShakeGesture1 method.

Joe13M
  • 21
  • 3
1

try this, it worked for me,

protected override void OnBackKeyPress(CancelEventArgs e)
{
    e.Cancel = false;
    ShakeGesturesHelper.Instance.ShakeGesture -= new EventHandler<ShakeGestureEventArgs>(Instance_ShakeGesture1);
}

Because you should delete the events when you leaving first page. So you can clean hakeGestureEventArgs when back key button is pressed.

  • Welcome! I would suggest to explain your solution a bit more so it is easier to understand what your are proposing and why it could solve the question, instead of just dropping code. – veducm Jan 29 '14 at 10:32
  • you should add joe's answer in below also – Clinton Ward Apr 02 '14 at 00:20
0

Okay my bad. Didn't know that you needed it to work multiple times.

Try this and let me know if it works good:

Write the same line of code that you've added, inside the OnNavigatedFrom method and delete it from your current method('Instance_ShakeGesture2')

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
Joe13M
  • 21
  • 3
  • i'm sorry I think its my bad too, I forget to tell you I using pivot item for every page and how I use OnNavigatedFrom method in pivot item? – pacifistazero Aug 28 '12 at 09:42
  • okay. In that case you could use the `SelectionChanged` event on the pivot page and do a check on the SelectedIndex. `if(SelectedIndex==1)` remove your `ShakeGesture1` event by using the '-=' code and follow likewise `if(SelectedIndex==0)` – Joe13M Aug 28 '12 at 12:56