开发者

Detecting tap events in quick succession

I have page which shows either one of a number of images or a button. On clicking the button I hide it and display the next image in the list. Tapping the image makes it disappear and shows the button again.

When clicking the button multiple times in quick succession the displayed image doesn't respond to being tapped. I guess it is due to UI thread blocking but I tried it with worker thread and still is not working.

public void ShowNextImage()
    {

image_new.Source = getCurrentBitmap();
 workerThread.WorkerReportsProgress = true;
        workerThread.WorkerSupportsCancellation = true;
        workerThread.DoWork += new DoWorkEventHandler(worker_DoWork);
        workerThread.RunWorkerCompleted += new      RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
 workerThread.RunWorkerAsync();
  }
  void worker_DoWork(object sender, DoWorkEventArgs e)
    {

        Deploymen开发者_开发技巧t.Current.Dispatcher.BeginInvoke(() =>
        {

                transition.Begin();
                transition.Completed += delegate
                {
                    transition.Stop();
                };


        });


    }
    void worker_RunWorkerCompleted(object sender,
                           RunWorkerCompletedEventArgs e)
    {


        if (e.Error != null)
        {
            // Happens on the UI thread so its ok
            MessageBox.Show("Error occurred...");

        }
    }


I recommend you in install the Windows Phone Toolkit from Codeplex and use the GestureListener. This lets you detect many things not just Tap and DoubleTap, it will also support pinch sizing and dragging.

Make sure in open the source code in VS2010 and take a look at the GestureSample.xaml you could probably just lift most of that into your app.


My guess is that you're delegate code is causing problems:

  transition.Completed += delegate
            {
                transition.Stop();
            };

My guess is that among all the thread context switches the Completed event is getting fired while you are just about starting the next image transition.

There are a few things you could try to help with this:

  • definitely only subscribe to completed once per transition
  • try using separate transition instances for each actual transition
  • try protecting the button so that it can't be pressed while the transition is occurring
  • try letting the transition stop on its own (they don't have to repeat?)
  • try reorganising the transition logic so that its not quite so multithreaded (not sure what else is going on in the app - I understand this may not be entirely possible if you've got lots of other things going on)
  • try using VisualStateManager state changes instead of these manually crafted transitions

Don't try them all together :)

Of course, my guess might be wrong... it might be something else...


Reading this again... another possibility is that something is being drawn over the top of the button and that this is capturing the touch events.

One basic suggestion is:

  • get rid of the workerthread code - go back to just UI code
  • put some simple Debug.WriteLine code in the Button_Click handler

Using this will tell you whether the Button is still responding to the click events or whether something more complicated has occurred. My experience is that normally the Button's keep on working at the click level now matter how many times you hit them - so something else is going wrong either in the click logic itself or in something else being shown over the top of the button, preventing it from being clicked.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜