Silverlight中没有鼠标的Click以及DoubleClick事件,至少到目前Silverlight Beta 2中还没有。希望在正式版中有相关事件处理(正式版中有太多期待了呵)。
参考了这篇blog,稍微修改了里面的一点代码,让所有FrameworkElement都可以产生Click及DoubleClick事件。由于不知道怎么添加附件,只好把所有代码贴这里,其中添加一些关键点的注释:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
MouseClickManager.cs
public class MouseClickManager
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public event MouseButtonEventHandler Click;
public event MouseButtonEventHandler DoubleClick;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Gets or sets a value indicating whether this <see cref="MouseClickManager"/> is clicked.
/// </summary>
/// <value><c>true</c> if clicked; otherwise, <c>false</c>.</value>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private bool Clicked
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Gets or sets the control.
/// </summary>
/// <value>The control.</value>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public FrameworkElement Control
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Gets or sets the timeout.
/// </summary>
/// <value>The timeout.</value>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int Timeout
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Initializes a new instance of the <see cref="MouseClickManager"/> class.
/// </summary>
/// <param name="control">The control.</param>
public MouseClickManager(FrameworkElement control, int timeout)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Clicked = false;
this.Control = control;
this.Timeout = timeout;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Handles the click.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
public void HandleClick(object sender, MouseButtonEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lock(this)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.Clicked) //之前已经得到一个Click,再次得到视为DoubleClick
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Clicked = false;
OnDoubleClick(sender, e);
}
else //捕获Click事件
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Clicked = true;
//创建线程来对标志位进行置位
ParameterizedThreadStart threadStart = new ParameterizedThreadStart(ResetThread);
Thread thread = new Thread(threadStart);
thread.Start(e);
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Resets the thread.
/// </summary>
/// <param name="state">The state.</param>
private void ResetThread(object state)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//就是说超过在第一个Click之后超过间隔时间没有收到第二个Click则认为是Click
Thread.Sleep(this.Timeout);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
lock (this)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.Clicked)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Clicked = false;
OnClick(this, (MouseButtonEventArgs)state);
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Called when [click].
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
private void OnClick(object sender, MouseButtonEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MouseButtonEventHandler handler = Click;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (handler != null)
this.Control.Dispatcher.BeginInvoke(handler, sender, e);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Called when [double click].
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
private void OnDoubleClick(object sender, MouseButtonEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MouseButtonEventHandler handler = DoubleClick;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (handler != null)
handler(sender, e);
}
}
实际用的时候:
1. 把MouseClickManage.cs类引入到工程中;
2. 用需要获得Click或DoubleClick的FrameworkElement初始化MouseClickManager,并指定时间间隔(200ms挺不错)
3. 在FrameworkElement的MouseLeftButtonUp事件中调用MouseClickManager.HandleClick()方法
看代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Application Use
public partial class Page : UserControl
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private MouseClickManager mcm = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Page()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Page_Loaded);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void Page_Loaded(object sender, RoutedEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//初始化MouseClickManager
this.mcm = new MouseClickManager(this.LayoutRoot, 200);
mcm.Click += new MouseButtonEventHandler(mcm_Click);
mcm.DoubleClick += new MouseButtonEventHandler(mcm_DoubleClick);
this.LayoutRoot.MouseLeftButtonUp += new MouseButtonEventHandler(LayoutRoot_MouseLeftButtonUp);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void LayoutRoot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//调用HandleClick方法
mcm.HandleClick(sender, e);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void mcm_DoubleClick(object sender, MouseButtonEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.ClickMessage.Text = "Double click!";
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void mcm_Click(object sender, MouseButtonEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.ClickMessage.Text = "Click!";
}
}
就这样用吧。