Strange in ShakeGesture Library on Windows Phone Application

ぐ巨炮叔叔 提交于 2019-12-13 20:36:22

问题


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 :)


回答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.




回答2:


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.




回答3:


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')



来源:https://stackoverflow.com/questions/12026663/strange-in-shakegesture-library-on-windows-phone-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!