Silverlight Hosted in Winforms

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 datatypes around. But if you needed to do so, you could always use serialization.

Calling Silverlight functions from your form seems to be the tricker direction. 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.

转载于:https://www.cnblogs.com/sofire/archive/2011/01/03/1924386.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值