WPF touchscreen events not working properly - wpf

I've created a WPF touchscreen application at work.
When I use a mouse, it behaves as you would expect. However, when using the touchscreen it doesn't behave as it should. It will regularly lag on a PreviewMouseDown touch event, and sometimes won't actually respond at all. Other times it works fine.
Does anybody know what could cause this behaviour, and what can I do to resolve it?
Many thanks

In researching the issue for someone over at EE, I ran across this post. Essentially, the PreviewTouchDown event does not occur until a finger touches the screen and moves. The answering post author goes on to say
To cause the PreviewTouchDown event to occur as soon as a finger touches the screen, set the Stylus.IsPressAndHoldEnabled attached property to false for this element.
This solution was posted in early 2014 and hasn't been rebutted, so it should work.

Related

Is there a SurfacePopup control in Surface 2?

We've been working on an application for the last few months that's aimed at Windows 7 tablet PCs. So we've used the Surface 2 SDK for most controls and it's all touch-happy.
I have noticed recently, though, that one of our custom controls isn't working as it should. This control provides popout menus, and these are achieved through the Popup control. On a developer's laptop, this works fine and the menus vanish when you click away from them. I've noticed, though, that on our test tablet they have a tendency to stay open.
I found that there was a SurfacePopup in the first Surface SDK, but I can't find one in the Surface 2 SDK. Did they get rid of it? Is there a 'best practice' approach?
If there's no simple solution, I may have to go old-school and add a window-sized hidden SurfaceButton below the menu when it appears, that hides itself and the menu when clicked or touched.
Beyond that I've noticed that sometimes the SurfaceScrollViewer within the popups won't work. I'm guessing this is because it's not picking up touch events properly. I tried adding this extension method to the window..
this.EnableSurfaceInput();
..but I get a NullReferenceException on System.Windows.Input.Mouse.get_LeftButton() which bizarrely suggests that it can only enable surface inputs for controls when there's a mouse plugged in.
Any ideas? They'll all be welcomed with open arms!
There's no SurfacePopup in the Surface SDK 2.0, however you can use a normal WPF popup. Then you need to make sure that it receives Touch Events by using the extension method you suggested above on the popup, not the window:
((HwndSource)HwndSource.FromVisual(popup)).EnableSurfaceInput();
Edit: As I just found out, this only works when the popup is initially open. To get it to work when the popup is opened later on, you don't need to use the popup, but the parent of it's child (see this question).
For the benefit of Daniel, and anyone else who needs a solution to this, I'll try to cast my mind back two years and explain how we got this working.
As far as I can remember, the answer was to use an adorner layer instead of a popup. Basically, every WPF control has an adorner layer, which sits above the control's UI stack. By default it contains nothing, but you can add whatever you like to it.
I got this all working by writing a custom control that allows you to place that control, with content, in the XAML and then show and hide it whenever you need to. When it's shown, it moves its contents into the adorner layer of the containing window, and when it's hidden it moves the contents back into the control itself, which is hidden from the user.
Afraid I can't go into any more detail than that, but as far as I can remember this was the ultimate solution; replacing popups (which never quite worked very well) with a custom control that uses the adorner layer.
Hope that helps!

MouseLeftButtonUp Alternative - Windows Mobile

Is there any other sensible way where I can fire a touch event on a Windows Mobile App?
I have a Hangman game. I have a bunch of images which are the alphabet.
I've put all of the images inside a Scroll Viewer so the user can scroll through the list.
As a precaution, I've set the click event to MouseLeftButtonUp because if I set it to "Down", it will trigger the image code when really, the user just wanted to scroll.
However, even with MouseLeftButtonUp, I've still got the same problem. When I test it, it scrolls but if the finger is still on the same image and if I release my finger, it'll fire off the code behind that image...
How can I overcome this problem? I hope my situation makes clear sense.
Perhaps you want to use touch events rather than mouse events since Windows mobiles devices are designed for touch rather than mouse clicking.

SketchFlow WP7 Navigation Buggy

I'm making a Sketchflow WP7 application which is working fine, except Navigation is sometimes buggy. Sometimes when I go to a screen and try and click it doesn't work, some times it does. I'm using the MouseLeftButtonDown Event to trigger it. After it 'freezes' I can no longer navigate via the map either, though before it freezes I can. I'm not sure if any errors are thrown because I'm not aware of a real time silverlight console. Sketchflow doesn't have a 'Debug Project' option only a 'Run Project' one.
It's very unpredictable, sometimes it works, sometimes it doesn't (though some links are more unreliable than others). Sometimes it gets stuck in different screens. I've tested this across browsers and computers and while sometimes a new browser occasionally has a better success rate, something always gets stuck.
I'm afraid I'm probably leaving out some details so feel free to ask for them. I'm not really very familiar with Silverlight and Sketchflow, I'm just working on a prototype so it seems like I shouldn't have to deal with any code based issues...
Thanks!
You might want to look for a Click Event rather than MouseButtonDown as that could be anything, it doesnt mean MouseButton Down, MouseButton Up, which resembles a click. this is just the same as if you use your finger to touch the screen, this would also trigger the MouseButton Down Eventhandler
so if I was you try and use the Click eventhandler for your navigation.
You can open the same project in VS (including the free express editions) and run it in the debugger from there to see if an exception is being thrown. That might help narrow down the problem.

WebView containing Silverlight

In my cocoa application I have a WebView. If I set that WebView to display a web page that contains Silverlight, the mouse move event never fires in that Silverlight application (with the exception of if I hold down the mouse button while moving). Everything else seems to work fine (clicking, interacting with objects) except for mouse move. Anyone have any ideas what is causing this and how to fix it?
Found out what the problem was. When Silverlight is embedded in a WebView (from WebKit) appears to have different concerns regarding threading (Firefox on OS X does not exhibit this problem). The solution was making sure that anything that touched a UI Object was in a control.Dispatcher.BeginInvoke (specifically in my case I was changing the text on a TextBlock in response to a MouseMove and that was causing the whole MouseMove to error out).
Hope this helps someone.

Making a WPF application retain focus at all times

I've got an issue with a WPF application that I'm writing. The app needs to be able to keep focus at all times. The computer it's running on is a highly specialized machine with the only purpose of running this application.
There is no keyboard connected to the machine (it has a touch screen), so the only thing that can steal focus is windows own "needy windows", such as windows update etc.
How can I make it so that my app retains focus at all times? Is it possible to make the entire app modal?
EDIT:
Thank you both for your answers. I think I'll end up using Topsmost for now, but I'll definitely check out the source of babysmash as that application works exactly the way I want mine to, in regards to the way it handles focus.
Look at the source of BabySmash. It is specifically designed to keep focus even under quite bizar circumstances. (It is a program designed to run at full screen and let babys smash on a keyboard - so quite some focus went into capturing all kinds of weird keyboard combinations and alert messages).
I would use
<Window ... Topmost="True">
i xaml. But maybe this is not what you are looking for

Resources