Expression Blend onLongClick event

340 views Asked by At

I wanna do a very simple thing using Expression Blend (SketchFlow).

I want to have a button in a screen that when it's pressed for more than 3 seconds it should go to another screen.

I thought about using this (green answer): Button Long Click

within the MouseLeftButtonDown but I don't know how to change to another screen using c# code...just by setting the "Navigate to" option.

So if anyone could tell me how to set a long click behavior in a button so it changes to a new screen it would be great.

1

There are 1 answers

2
Chuck Hays On BEST ANSWER

Here is a similar but simpler class you can use:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using System.Windows.Threading;

namespace SilverlightApplication14
{
    public class LongClickButton : Button
    {
        public event EventHandler LongClick;

        public static DependencyProperty HowLongProperty = DependencyProperty.Register("HowLong", typeof(double), typeof(LongClickButton), new PropertyMetadata(3000.0));

        public double HowLong
        {
            get
            {
                return (double)this.GetValue(HowLongProperty);
            }
            set
            {
                this.SetValue(HowLongProperty, value);
            }
        }

        private DispatcherTimer timer;

        public LongClickButton()
        {
            this.timer = new DispatcherTimer();
            this.timer.Tick += new EventHandler(timer_Tick);
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            this.timer.Stop();
            // Timer elapsed while button was down, fire long click event.
            if (this.LongClick != null)
            {
                this.LongClick(this, EventArgs.Empty);
            }
        }

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            this.timer.Interval = TimeSpan.FromMilliseconds(this.HowLong);
            this.timer.Start();
        }

        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonUp(e);
            this.timer.Stop();
        }
    }

}

With this, you can use any of the standard behaviors in Blend along with the new LongClick event. Set the HowLong property to the number of ms you want (default is 3000) and then use an eventtrigger set to LongClick to trigger your navigation:

<local:LongClickButton Margin="296,170,78,91">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="LongClick">
                <ei:ChangePropertyAction PropertyName="Background" TargetName="LayoutRoot">
                    <ei:ChangePropertyAction.Value>
                        <SolidColorBrush Color="#FFFF1C1C"/>
                    </ei:ChangePropertyAction.Value>
                </ei:ChangePropertyAction>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </local:LongClickButton>