.net 在silverlight中禁用右键单击

.net 在silverlight中禁用右键单击,.net,silverlight,.net,Silverlight,我们在信息亭类型的场景中使用silverlight。是否有方法禁用右键单击功能以进入silverlight配置对话框?//在SharePoint中,我添加了一个小代码,告诉SP在加载每个部件后运行脚本。工作就像一个符咒:) //编辑 或者更好的是,silverlight论坛建议您这样做: _spBodyOnLoadFunctionNames.push('setupElement'); 函数setupElement() { document.getElementById('silverligh

我们在信息亭类型的场景中使用silverlight。是否有方法禁用右键单击功能以进入silverlight配置对话框?

//在SharePoint中,我添加了一个小代码,告诉SP在加载每个部件后运行脚本。工作就像一个符咒:)

//编辑

或者更好的是,silverlight论坛建议您这样做:


_spBodyOnLoadFunctionNames.push('setupElement');
函数setupElement()
{
document.getElementById('silverlightObjDiv')。oncontextmenu=disableRightClick;
}
功能禁用右键单击(e){
如果(!e)e=window.event;
如果(如默认){
e、 预防默认值();
}否则{
e、 returnValue=false;
}
}

在Silverlight 4中,您可以在C#中完成此操作,而无需摆弄和依赖任何HTML

下面的示例显示了如何实现控件实际使用的右键单击,但如果只想禁用,则可以创建一个clicktrap

 public partial class MainPage : UserControl
 {
      public MainPage()
      {
          InitializeComponent();

          // wire up the event handlers for the event on a particular UIElement
          ChangingRectangle.MouseRightButtonDown += new MouseButtonEventHandler(RectangleContextDown);
          ChangingRectangle.MouseRightButtonUp += new MouseButtonEventHandler(RectangleContextUp);
      }

     void RectangleContextUp(object sender, MouseButtonEventArgs e)
     {
         // create custom context menu control and show it.
         ColorChangeContextMenu contextMenu = new ColorChangeContextMenu(ChangingRectangle);
         contextMenu.Show(e.GetPosition(LayoutRoot));
     }

     void RectangleContextDown(object sender, MouseButtonEventArgs e)
     {
         // handle the event so the default context menu is hidden
         e.Handled = true;
     }
 }

参考资料:

正如Dain所提到的,在Silverlight 4中,您可以轻松做到这一点:

使控件无窗口:

<param name="windowless" value="true" />
捕获

在Firefox和Chrome中,您必须在上下文菜单和鼠标滚轮滚动功能之间进行选择。遗憾的是,您不能同时拥有这两个控件,希望Silverlight 5中的情况会有所改变。

控件必须是无窗口的吗?@Niall是的,它必须填充整个屏幕。谢谢dain,这是一个更好的答案,在我看来-作为参考,“ChangingRectangle”是MainPage.xaml中讨论的控件的x:Name
<param name="windowless" value="true" />
public MainPage()
{
    LayoutRoot.MouseRightButtonDown += (s, e) => { e.Handled = true; };
}