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

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

问题描述

我想通过 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 窗体项目中的内容.在您的 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" 添加到您的 代码>元素:

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

注意到 Page 属性了吗?这就是对 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>

现在我们可以像这样从 Windows 窗体应用程序调用 CallMe() 方法:

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

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

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