-1

在开始之前,我必须说一下。我读过这个问题。而且我看不出它与我几个小时前发布的这个问题的重复。

我原来的帖子是这样的:

我在 WPF 中使用了一些按钮来显示来自各种文件夹的图像。所有按钮的Click模式都是Hover。对于某些按钮,悬停后,它被路由到一个函数OnButtonClick。我想要做的是,只有在鼠标悬停在按钮上X几秒钟后,OnButtonClick才应该完成函数内部的处理。

XAML 代码:

    <Button Name="Dresses" MouseEnter="onMouseEnter" MouseLeave="onMouseLeave" Content="Dresses" Grid.Row="2" Height="25" Width="85" VerticalAlignment="Center" Grid.Column="5"  FontFamily="Tahoma" FontSize="14" FontWeight="Bold" HorizontalAlignment="Center" Cursor="Hand" ClickMode="Hover">
        <Button.Background>
            <LinearGradientBrush>
                <GradientStop Color="Yellow" Offset="0" />
                <GradientStop Color="Green" Offset="1" />
            </LinearGradientBrush>
        </Button.Background>
    </Button>

C#代码:

    private void OnButtonClickDresses(object sender, RoutedEventArgs e)
    {
            //Code for delay

            //Code which should run only if mouse on button after 5 seconds

    }

PS:我是 WPF 和 C# 的初学者。因此,如果您可以发布一个最低限度的工作示例,我将非常感激。

4

4 回答 4

2

这是给您的示例应用程序。

using System.Windows.Threading;

namespace MyWPF App
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>

public partial class MainWindow : Window
{

    DateTime dt;
    DispatcherTimer t;

    public MainWindow()
    {

        InitializeComponent();
        t = new DispatcherTimer();
        t.Tick += new EventHandler(t_Tick);

    }
    private void button1_MouseEnter(object sender, MouseEventArgs e)
    {
        dt=DateTime.Now;
        t.Interval = new TimeSpan(0, 0, 1);
        t.IsEnabled = true;


    }

    void t_Tick(object sender, EventArgs e)
    {

        if ((DateTime.Now - dt).Seconds >= 5)
        {
            MessageBox.Show("Hello");// Here you can put your code which you want to execute after 5 seconds.
        }

    }

    private void button1_MouseLeave(object sender, MouseEventArgs e)
    {
        t.IsEnabled = false;
    }
}

}
于 2012-10-16T11:07:04.827 回答
2

您可以使用 MouseEnter 启动一个间隔为 X 秒的 Timer。在 MouseLeave 上,您停止此计时器。现在您将 OnButtonClickDresses 方法中的代码放入 Timer_Tick 方法中。

样本:

public partial class MainWindow : Window
{
    DispatcherTimer dt;

    public MainWindow()
    {
        InitializeComponent();
        dt = new DispatcherTimer();
        dt.Interval = new TimeSpan(0, 0, 5);//wait for 5 Seconds
        dt.Tick += new EventHandler(dt_Tick);
    }

    void dt_Tick(object sender, EventArgs e)
    {
        //do code
    }

    private void button1_MouseEnter(object sender, MouseEventArgs e)
    {
        dt.Start();
    }

    private void button1_MouseLeave(object sender, MouseEventArgs e)
    {
        dt.Stop();
    }
}

编辑:

    <Button Name="Dresses" MouseEnter="button1_MouseEnter" MouseLeave="button1_MouseLeave" Content="Dresses" Grid.Row="2" Height="25" Width="85" VerticalAlignment="Center" Grid.Column="5"  FontFamily="Tahoma" FontSize="14" FontWeight="Bold" HorizontalAlignment="Center" Cursor="Hand"
        <Button.Background>
            <LinearGradientBrush>
                <GradientStop Color="Yellow" Offset="0" />
                <GradientStop Color="Green" Offset="1" />
            </LinearGradientBrush>
        </Button.Background>
    </Button>
于 2012-10-16T11:19:06.577 回答
0

您不能在方法中使用它:

System.Threading.Thread.Sleep (5000);

因为这会冻结你的界面。

为此,您应该使用某种异步任务。我建议:

Task.Factory.StartNew (() =>
{
   System.Threading.Thread.Sleep (5000);
})
. ContinueWith (() =>
{
     / / Code after 5 seconds
}
TaskScheduler.FromCurrentSynchronizationContext ());

虽然我不认为它是理想的,因为它可以将鼠标移开,然后放回去。这可能会导致混乱。

我推荐的方法:

使用事件 MouseEnter 和 MouseLeave,以及类

System.Diagnostics.Stopwatch 
System.Windows.Threading.DispatcherTimer

我不完全理解你需要的逻辑,但我认为通过这两个类和事件你可以做你想做的事。

于 2012-10-16T11:21:12.120 回答
0

这似乎适用于任意数量的按钮......

XAML:

    <Grid>
        <Button Name="myButton1" Content="Button 1" HorizontalAlignment="Left" Margin="34,51,0,0" VerticalAlignment="Top" Width="75" Click="HoverButton_Click" MouseEnter="HoverButton_MouseEnter" MouseLeave="HoverButton_MouseLeave"/>
        <Button Name="myButton2" Content="Button 2" HorizontalAlignment="Left" Margin="150,51,0,0" VerticalAlignment="Top" Width="75" Click="HoverButton_Click" MouseEnter="HoverButton_MouseEnter" MouseLeave="HoverButton_MouseLeave"/>
        <TextBox Name="myTextBox" HorizontalAlignment="Left" Height="147" Margin="34,91,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="232"/>
    </Grid>

代码:

    private Dictionary<Button, System.Threading.Timer> buttonTimers = new Dictionary<Button, System.Threading.Timer>();

    public MainWindow()
    {
        InitializeComponent();
    }

    private void HoverTime(object state)
    {
        var thisButton = state as Button;

        thisButton.Dispatcher.BeginInvoke((Action)delegate()
        {
            thisButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
        });
    }

    private void HoverButton_Click(object sender, RoutedEventArgs e)
    {
        var button = sender as Button;

        myTextBox.Text += "Delayed hover: " + button.Content + Environment.NewLine;
    }

    private void HoverButton_MouseEnter(object sender, MouseEventArgs e)
    {
        var thisButton = sender as Button;

        var t = new System.Threading.Timer(new TimerCallback(HoverTime), sender, 2500, Timeout.Infinite);

        buttonTimers.Add(thisButton, t);
    }

    private void HoverButton_MouseLeave(object sender, MouseEventArgs e)
    {
        var thisButton = sender as Button;

        if (buttonTimers.ContainsKey(thisButton))
        {
            buttonTimers[thisButton].Dispose();
            buttonTimers.Remove(thisButton);
        }
    }
于 2012-10-16T12:04:33.843 回答