Silverlight hosted in winforms

Source: Internet
Author: User
I think using the Windows Forms webbrowser control is your best bet. To do this, you'll need your Silverlight app on a webpage, then you point your webbrowser at the page's uri.

To keep your webbrowser control from acting like IE, I 'd recommend setting the following:

Webbrowser. allownavigation = false; webbrowser. allowwebbrowserdrop = false; webbrowser. iswebbrowsercontextmenuenabled = false; webbrowser. webbrowsershortcutsenabled = false;

Calling methods on your form from within Silverlight is easy enough to do. to start, you need a class that has all the methods you want to call from Silverlight. you can use your form itself or another object, but you need to mark the class with the [comvisible (true)] attribute. then you assign your object to the webbrowser. objectforscripting property. this exposes your object as "window. external "on the webpage.

[comvisible ( true )] Public partial class form1: Form
{......
webbrowser. objectforscripting = This ;
...... Public void callmeinform ( string something) {
MessageBox. show ( "Silverlight said:" + something);
}

That's it for inside your Windows Forms project. inside of your Silverlight app, you need to pick up this objectforscripting and invoke methods on it. to call the method in my example above, use the following lines:

Using system. windows. browser ;...... scriptobject myform = (scriptobject) htmlpage. window. getproperty ("external"); myform. invoke ("callmeinform", "Testing 1 2 3 ");

The invoke command lets you pass any number and type of parameters to your function, although I suspect it wouldn't like it very much if you try passing complex PES ypes around. but if you needed to do so, you coshould always use serialization.

Calling Silverlight functions from your form seems to be the tricker ction. I haven't figured this one out completely yet.

In your Silverlight app, you also expose functions to the webpage. to do this, use the htmlpage. registerscriptableobject () function. again, you can pass in any class with methods you want to expose. for a method to be exposed, though, you have to mark it with the [scriptablemember] attribute.

Htmlpage. registerscriptableobject ("page", this );...... [scriptablemember] public void callmeinsilverlight (string message) {htmlpage. window. alert ("the form said:" + message );}

At this point, your method is exposed to JavaScript on the page. unfortunately, I have not yet been able to get the webbrowser control to actually call the method. once I figure out a good way to do that, I'll come back and finish this example.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.