Silverlight在Winforms中托管 [英] Silverlight Hosted in Winforms

查看:128
本文介绍了Silverlight在Winforms中托管的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过winforms浏览器在winforms中托管一个silverlight控件,但是为了工作,我需要一些方法让表单与silverlight交谈,以及相反的方式。是否有可能以两种方式使用JavaScript作为中间人相互交互?即,让表单与浏览器的javascript说话,并让它与silverlight控件说话?有没有更好的办法?甚至是一种方式? (除了将代码编译为silverlight和wpf)

I would like to host a silverlight control in winforms via a winforms browser, but for it to work I need some way for the forms to talk to the silverlight, and also the other way around. Would it be possible to somehow have the two interact with each other using JavaScript as a middleman? I.e., have the form speak to the browser's javascript, and have that speak to the silverlight control? Is there a better way? Or even a way at all? (other than compiling the code as silverlight and wpf)

推荐答案

我认为使用Windows窗体WebBrowser控件是最好的选择。为此,您需要在网页上使用Silverlight应用程序,然后将WebBrowser指向页面的URI。

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.

要保持WebBrowser控件不像IE,我建议设置以下内容:

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;

从Silverlight中调用表单上的方法很容易。首先,您需要一个具有要从Silverlight调用的所有方法的类。您可以使用表单本身或其他对象,但需要使用[ComVisible(true)]属性标记该类。然后将对象分配给WebBrowser.ObjectForScripting属性。这会在网页上将您的对象公开为window.external。

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);
    }
}

这就是Windows Forms项目中的内容。在Silverlight应用程序内部,您需要选择此ObjectForScripting并在其上调用方法。要在上面的示例中调用该方法,请使用以下行:

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");

Invoke命令允许您将任意数量和类型的参数传递给您的函数,尽管我怀疑它不会如果你尝试传递复杂的数据类型,那就非常喜欢它。但是如果你需要这样做,你总是可以使用序列化。

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.

从你的表单调用Silverlight函数似乎是转向方向。我还没完全想到这一点。

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

在你的Silverlight应用程序中,你还将功能暴露给网页。为此,请使用HtmlPage.RegisterScriptableObject()函数。同样,您可以使用要公开的方法传入任何类。但是,对于要公开的方法,您必须使用[ScriptableMember]属性进行标记。

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);
}

此时,您的方法在页面上暴露给JavaScript,您可以假设您将 id =silverlightControl添加到< object> 元素中,就这样称呼它:

At this point, your method is exposed to JavaScript on the page and you could call it like so, assuming you added id="silverlightControl" to your <object> element:

document.getElementById('silverlightControl').Content.Page.CallMeInSilverlight("testing 1 2 3");

请注意页面属性?这就是对 RegisterScriptableObject()的调用给了我们的。现在,让我们将它包装成一个整洁的JavaScript方法:

Notice the Page property? That's what the call to RegisterScriptableObject() gave us. Now, let's wrap this into a tidy JavaScript method:

<script type="text/javascript">
    function CallMe(message) {
        var control = document.getElementById('silverlightControl');
        control.Content.Page.CallMeInSilverlight(message);
    }
</script>

现在我们可以调用 CallMe()来自Windows窗体应用程序的方法如下:

And now we can call the CallMe() method from the Windows Forms app like so:

public void CallToSilverlight()
{
    webBrowser.InvokeScript("CallMe", new object[] { "testing 1 2 3" });
}

这篇关于Silverlight在Winforms中托管的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆