开发者

WPF button press on Windows 7 mulit-touch

开发者 https://www.devze.com 2023-01-17 15:56 出处:网络
I am developing a WPF kiosk like client that will be deployed on an HP TouchSmart Windows 7 machine with multi-touch enabled.My problem is that with Windows 7 multi-touch, the application does not rec

I am developing a WPF kiosk like client that will be deployed on an HP TouchSmart Windows 7 machine with multi-touch enabled. My problem is that with Windows 7 multi-touch, the application does not recognize a finger 'tap' as a button press event, and therefore the button press Trigger to change color is never fired.

The Windows 7 animation for the touch usually displays, and the button click event fires fine. It is only the XAML define style for the 'IsPressed' event that does not function as intended on a finger tap. It does eventually work if enough pressure is applied with a finger and/or you roll or press like you would do for a fingerprint. Is there a workaround for to make a 'tap' fire a press/click event?开发者_StackOverflow社区

<Trigger Property="AreAnyTouchesOver" Value="true">
    <Trigger.ExitActions>
        <BeginStoryboard Storyboard="{StaticResource PressedOff}" />
    </Trigger.ExitActions>
    <Trigger.EnterActions>
        <BeginStoryboard Storyboard="{StaticResource PressedOn}" />
    </Trigger.EnterActions>
</Trigger>

<Trigger Property="AreAnyTouchesDirectlyOver" Value="true">
    <Trigger.ExitActions>
        <BeginStoryboard Storyboard="{StaticResource PressedOff}" />
    </Trigger.ExitActions>
    <Trigger.EnterActions>
        <BeginStoryboard Storyboard="{StaticResource PressedOn}" />
    </Trigger.EnterActions>
</Trigger>


Simply set the Stylus.IsPressAndHoldEnabled attached property to False for the button. This will cause the button's IsPressed property to be set/cleared in response to a touch as well as a mouse click.

From the documentation for the UIElement.TouchDown Event:

To cause the PreviewTouchDown and TouchDown events to occur as soon as a finger touches the screen, set the Stylus.IsPressAndHoldEnabled attached property to false for this element.

For example, you could use a setter in your button style to make sure the property is set to false:

<Setter Property="Stylus.IsPressAndHoldEnabled" Value="False"/>

I found this question while trying to find a way to make my button reliably animate for both mouse clicks and touch events. The accepted answer sent me down what seems to be the wrong path: Synchronizing storyboards with touch up/down events. Using the IsPressed property trigger for both has proven to be simple and reliable. Hopefully this saves someone else from making a similar mistake.


I am not clear on what you want t do.

Some explanation on your issue:

In touch IsPressed is only triggered when your contact is held down over the element and the movement threshold is passed to create a "press" (That is why you see it if you press the finger down and slide it slightly)

As soon as the contact comes up IsPressed = false.

When you "tap" a button you are in essence doing a mouse click. This is why events fires as expected.

So what exactly are you trying to do? You want to fire an storyboard animation when a button is "held down" ("IsPressed") or when a button is clicked (tapped).

These are two different situations, also note that often times desktop metaphors do not translate well to the touch environment. You may want to rethink how you are giving visual feedback to the user.

I think the easiest answer for what you want to is Trigger on "TouchDown" if I understand your questions correctly.

UPDATE:

This should work for a TouchDown trigger

<Button Content="Touch Down Example" Height="20" Width="20">
        <Button.Triggers>
            <EventTrigger RoutedEvent="Button.TouchDown">
                <BeginStoryboard>
                    ....
                </BeginStoryboard>
            </EventTrigger>
        </Button.Triggers>
    </Button>

NOTE: I don't have a touch screen in front of me so there could be errors but it looks correct.


For touch development, I would recommend using the Microsoft® Surface® Toolkit for Windows Touch Beta in your project, and using the surface controls included, since they handle touch events properly, and a multitude of use cases, such as a user pressing a button with 2 fingers, and lifting them one at a time. Only the last "lift" or TouchUp event should trigger a Click event.

All of the surface controls are subclasses of the Windows controls, so SurfaceListBox subclasses ListBox, SurfaceButton subclasses Button, etc. This way, all your styles and templates will still work with these controls.

In your case, you will want to use the SurfaceButton control. Simply add a reference to the toolkit assemblies, and in your outer element (, , etc), add a reference to the surface namespace, such as

xmlns:s="http://schemas.microsoft.com/surface/2008"

and then add the surface controls from that namespace, such as

<s:SurfaceButton x:Name="button1" Click="OnButtonClick" />
0

精彩评论

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