前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何在silverlihgt中使用右键

如何在silverlihgt中使用右键

作者头像
用户1172164
发布2018-01-16 13:48:26
6070
发布2018-01-16 13:48:26
举报
文章被收录于专栏:我和未来有约会

一般我们在silverlight中点击右键会出现如下的对话筐.

在flash中 其提供了一个可定制话的右键菜单系统.(ContextMenu)

这个在silverlight中也是一样可以做到的.

过程如下:

1.添加一个<TextBlock>到Page.xaml中

<UserControl x:Class="rightClick.Page"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     Width="400" Height="300"> <Grid x:Name="LayoutRoot" Background="White"> <TextBlock x:Name="MyField">Right click please.</TextBlock> </Grid> </UserControl>

2.在页面中设置silverlight的参数Windowless="true"

代码语言:js
复制
 <asp:Silverlight ID="Silverlight1" runat="server" Height="480px"

   MinimumVersion="2.0.30523" Source="~/ClientBin/rightClick.xap" Windowless="true" Width="640px"/>
 
代码语言:js
复制
<object data="data:application/x-silverlight," type="application/x-silverlight-2-b2" width="100%" height="100%">
<param name="source" value="ClientBin/rightClick.xap"/>
<param name="onerror" value="onSilverlightError"/>
<param name="background" value="white"/>
<param name="Windowless" value="true"/>
<a href="http://go.microsoft.com/fwlink/?LinkID=115261" style="text-decoration: none;">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>
</a>
</object>

3.最后修改Page.xaml.cs页面的代码

  • 新建立一个ContextMenuInterceptor类.这个类是用来处理页面中“OnContextMenu”事件的.在用到HTMLPage对象你需要引入System.Window.Browser命名空间.
  • 在调用e.PeventDefault()方法后,将会取消右键点击事件.所以silverlight不会捕捉到它.
  • 在这里我们已经成功的拦截了右键点击事件,做我们想做的任何事情了.;)

代码语言:js
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input; 
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
namespace rightClick
public partial class Page : UserControl
ContextMenuInterceptor _cmi = null;
public Page()
InitializeComponent();
_cmi = new ContextMenuInterceptor(MyField);
public class ContextMenuInterceptor
TextBlock TextField;
public ContextMenuInterceptor(TextBlock textField)
TextField = textField;
HtmlPage.Document.AttachEvent("oncontextmenu", this.OnContextMenu);
private void OnContextMenu(object sender, HtmlEventArgs e)
TextField.Text = "Right Clicked Blocked at " + e.OffsetX + "," + e.OffsetY;
//Cancels the event if it is cancelable.
e.PreventDefault();
 

  原文地址:http://silverlight.net/blogs/msnow/archive/2008/07/01/tip-of-the-day-14-how-to-right-click-on-a-silverlight-application.aspx

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2008-07-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档