Wiki


Wiki Table of Contents

Page Details

Published by:
3 people found this article useful.

100% of people found this useful
Silverlight Tip of the Day #14 – How to Right Click on a Silverlight Application.

Filed under: [Edit Tags]

Có lẽ bạn đã từng chú ý đến việc khi  click chuột phải lên 1 trình ứng dụng Silverlight  thì sẽ hiện ra một context menu và một  hộp thoại configuration ( hộp thoại cấu hình):

Context Menu:

Hộp thoại cấu hình:

Vậy điều gì sẽ xảy ra khi bạn muốn click chuột phải trên ứng dụng của bạn? Trong khi Silverlight lại không hỗ trợ chuột phải, có một cách khắc phục điều này. Tôi sẽ trình bày từng bước để làm sao tạo ra một  right click event ( sự kiện click chuột phải) , điều khiển nó và hiển thị lên những nội dung mà bạn muốn hiển thị thay vì hộp thoại cấu hình của Silverlight.

Bước 1. Để bắt đầu , hãy thêm 1 control  <TextBlock> vào trang  Page.xaml  của chúng ta để đặt sự kiện click chuột phải:

<UserControl x:Class="SilverlightApplication15.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="red">
        <TextBlock x:Name="MyField">Right click please.</TextBlock>
    </Grid>
</UserControl>

Bước 2. Tiếp theo, chúng ta cần gán Silverlight control sang  windowless. Mở trang web  (ví dụ  default.aspx) mà chứa  Silverlight control và thêm  tag Windowless=”true” vào đó . Ví dụ:

<asp:Silverlight ID="Xaml1" runat="server" Windowless="true" Source="~/ClientBin/SilverlightApplication15.xap" MinimumVersion="2.0.30523" Width="100%" Height="100%" />

Bước 3: Cuối cùng, hãy chú ý nhìn vào code chúng ta sẽ thêm vào trang Page.xaml.cs.

  • Tôi bắt đầu bằng việc tạo 1 lớp mới là  ContextMenuInterceptor. Trong hàm khởi tạo constructor chúng ta tạo 1 sự kiện   “OnContextMenu”  vào tài liệu của trang HtmlPage.  Và để sử dụng đối tượng HTMLPage bạn cần phải thêm vào namespace System.Window.Browser.
  • Sau đó tôi gọi  e.PeventDefault(). Điều này ngăn cản sự kiện click chuột lan truyền đi để Silverlight không thể nhận được sự kiện đó.
  • Vào lúc này, chúng ta đã chặn được right click và có thể làm bất cứ điều gì chúng ta muốn. Trong trường hợp của tôi, đơn giản chỉ là hiển thị tọa độ nơi tôi đã click chuột.

Page.xaml.cs:

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 SilverlightApplication15
{
    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;           
            e.PreventDefault();           
        }
    }
}


Thank you,
--Mike Snow

 

Recent Comments

Leave the first comment for this page.