简体   繁体   中英

Adding onClick event handler to Silverlight control?

My web site consumes a silverlight application using the application's xap file. Is it possible to catch the 'onclick' event that occurs when the user clicks on the silverlight control? I would like to re-focus the screen so that only the control is visible when this happens.

I have control over both the silverlight code and the web site code, so a solution from either direction would be equally appreciated.

Please note that I am very new to Silverlight so if any of the above sounded 'n00b-y'... well, it was. Forgive me:)

Find out your main XAML file of your Silverlight application by checking the RootVisual assignment statement in App.xaml.cs file.

private void Application_Startup(object sender, StartupEventArgs e)
{
    **this.RootVisual = new MainPage();**
}

By default, MainPage.xaml.cs is the first UserControl to be loaded on Application Startup.

Attach an event handler to the UserControl.MouseLeftButtonDown event in the MainPage constructor

public MainPage()
{
    InitializeComponent();
    **this.MouseLeftButtonDown += MainPage_MouseLeftButtonDown;**
}

In the event hander, invoke your javascript method "refocusScreen" (you need to implement this method the re-focus the screen) using Html Bridge

void MainPage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    // About [Invoke][2] method
    **HtmlPage.Document.Invoke("refocusScreen");**

    // detach event handler so that this won't call the JS method everytime the Mouse left button goes down.
    this.MouseLeftButtonDown -= MainPage_MouseLeftButtonDown;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM