在开始之前,我必须这样说。我看过this question。而且我不知道它是几个小时前发布的this question的副本。
我原来的帖子是:
我在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#的初学者。所以,如果你能发布一个最小的工作实例,我真的非常感激。
答案 0 :(得分: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;
}
}
}
答案 1 :(得分:2)
您可以使用MouseEnter启动间隔为X秒的计时器。在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>
答案 2 :(得分: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
我不完全理解你需要的逻辑,但我认为通过这两个类和事件,你可以做你想做的事。
答案 3 :(得分: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);
}
}