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

Context Menu:

Silverlihgt 2.0 参考:如何在silverlihgt中使用右键(How to Right Click on a Silverlight Application.)

Configuration Dialog:

Silverlihgt 2.0 参考:如何在silverlihgt中使用右键(How to Right Click on a Silverlight Application.)

 

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

 

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

过程如下:
Step 1. 添加一个<TextBlock>到Page.xaml中

 


    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>

 

Step 2. 在页面(.aspx 与 htm)中设置silverlight的参数Windowless="true",即我们需要设置Silverlight 控件为无视窗的(windowless).

(1) .aspx页面:

 
            MinimumVersion="2.0.30523" Source="~/ClientBin/rightClick.xap" Windowless="true" Width="640px" />

(2)htm页面

 


            <param name="source" value="ClientBin/rightClick.xap"/>
            
<param name="onerror" value="onSilverlightError" />
            
<param name="background" value="white" />
            
<param name="Windowless" value="true" />
            
<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>

 

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

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

 

 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();
        }
    }
}

 

运行一下,跑起来就可以了,无论你是用aspx作为起始页,还是用htm作为起始页,都可以看到下面效果:

Silverlihgt 2.0 参考:如何在silverlihgt中使用右键(How to Right Click on a Silverlight Application.)

 

右击文字区域:

Silverlihgt 2.0 参考:如何在silverlihgt中使用右键(How to Right Click on a Silverlight Application.)


 

 



相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-18
  • 2021-04-27
猜你喜欢
  • 2021-07-21
  • 2022-12-23
  • 2021-06-07
  • 2021-12-08
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案