Silverlight中没有鼠标的Click以及DoubleClick事件,至少到目前Silverlight Beta 2中还没有。希望在正式版中有相关事件处理(正式版中有太多期待了呵)。
参考了这篇blog,稍微修改了里面的一点代码,让所有FrameworkElement都可以产生Click及DoubleClick事件。由于不知道怎么添加附件,只好把所有代码贴这里,其中添加一些关键点的注释:

MouseClickManager.cs
public class MouseClickManager

{
public event MouseButtonEventHandler Click;
public event MouseButtonEventHandler DoubleClick;


/**//// <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>

private bool Clicked
{ get; set; }

/**//// <summary>
/// Gets or sets the control.
/// </summary>
/// <value>The control.</value>

public FrameworkElement Control
{ get; set; }


/**//// <summary>
/// Gets or sets the timeout.
/// </summary>
/// <value>The timeout.</value>

public int Timeout
{ get; set; }


/**//// <summary>
/// Initializes a new instance of the <see cref="MouseClickManager"/> class.
/// </summary>
/// <param name="control">The control.</param>
public MouseClickManager(FrameworkElement control, int timeout)

{
this.Clicked = false;
this.Control = control;
this.Timeout = timeout;
}


/**//// <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)

{
lock(this)

{
if (this.Clicked) //之前已经得到一个Click,再次得到视为DoubleClick

{
this.Clicked = false;
OnDoubleClick(sender, e);
}
else //捕获Click事件

{
this.Clicked = true;
//创建线程来对标志位进行置位
ParameterizedThreadStart threadStart = new ParameterizedThreadStart(ResetThread);
Thread thread = new Thread(threadStart);
thread.Start(e);
}
}
}


/**//// <summary>
/// Resets the thread.
/// </summary>
/// <param name="state">The state.</param>
private void ResetThread(object state)

{
//就是说超过在第一个Click之后超过间隔时间没有收到第二个Click则认为是Click
Thread.Sleep(this.Timeout);

lock (this)

{
if (this.Clicked)

{
this.Clicked = false;
OnClick(this, (MouseButtonEventArgs)state);
}
}
}


/**//// <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)

{
MouseButtonEventHandler handler = Click;

if (handler != null)
this.Control.Dispatcher.BeginInvoke(handler, sender, e);
}


/**//// <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)

{
MouseButtonEventHandler handler = DoubleClick;

if (handler != null)
handler(sender, e);
}
}
实际用的时候:
1. 把MouseClickManage.cs类引入到工程中;
2. 用需要获得Click或DoubleClick的FrameworkElement初始化MouseClickManager,并指定时间间隔(200ms挺不错)
3. 在FrameworkElement的MouseLeftButtonUp事件中调用MouseClickManager.HandleClick()方法
看代码:

Application Use
public partial class Page : UserControl

{
private MouseClickManager mcm = null;

public Page()

{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Page_Loaded);
}

void Page_Loaded(object sender, RoutedEventArgs e)

{
//初始化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);
}

void LayoutRoot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)

{
//调用HandleClick方法
mcm.HandleClick(sender, e);
}

void mcm_DoubleClick(object sender, MouseButtonEventArgs e)

{
this.ClickMessage.Text = "Double click!";
}

void mcm_Click(object sender, MouseButtonEventArgs e)

{
this.ClickMessage.Text = "Click!";
}
}
就这样用吧。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端