简体   繁体   English

Silverlight 4 MVVM:从 viewmodel 调用 Javascript function

[英]Silverlight 4 MVVM: Call Javascript function from viewmodel

we have developed an Intranet Management Application with Silverlight 4. We have been asked to add the functionality to call a remote desktop tool which is installed on clients using the Intranet SL App.我们使用 Silverlight 4 开发了一个 Intranet 管理应用程序。我们被要求添加调用远程桌面工具的功能,该工具安装在使用 Intranet SL App 的客户端上。 In an earlier version of the tool written in ASP.NET we just added a Javascript function to the aspx page like this:在用 ASP.NET 编写的工具的早期版本中,我们刚刚将 Javascript function 添加到 aspx 页面,如下所示:

function RunShellCommand()
 {
        var launcher = new ActiveXObject("WScript.Shell");
        launcher.Run("mstsc.exe");
    }

and called it from ASP.NET.并从 ASP.NET 调用它。

Now it's clear that SL4 is running in a sandbox and that I cant use the AutomationFactory to create a WScript.Shell object (out of browser mode is not an option).现在很明显 SL4 正在沙箱中运行,并且我无法使用 AutomationFactory 创建 WScript.Shell object(不能退出浏览器模式)。

I thought I could circle around the problem by, again, adding the RunShellCommand javascript method in the aspx page where the SL4 control is hosted and call it via我想我可以再次通过在托管 SL4 控件的 aspx 页面中添加 RunShellCommand javascript 方法来绕过这个问题,并通过调用它

HtmlPage.RegisterScriptableObject("Page", this);

HtmlPage.Window.Invoke("RunShellCommand", "dummydata");

from my ViewModel.从我的视图模型。 When I run the Application the debugger just skips the RegisterScriptableObject method and quits.当我运行应用程序时,调试器只是跳过 RegisterScriptableObject 方法并退出。 Nothing happens.什么都没发生。 My question is if am doing something wrong or if this just wont work this way.我的问题是如果我做错了什么,或者这不会以这种方式工作。 Is it possible that I cant do a RegisterScriptableObject from a viewmodel?是否有可能我不能从视图模型中执行 RegisterScriptableObject?

EDIT: When I explicitly put a try, catch block around the two methods I get an ArgumentException from the first method stating that the current instance has no scriptable members.编辑:当我明确地在两个方法周围放置一个 try, catch 块时,我从第一个方法中得到一个 ArgumentException,说明当前实例没有可编写脚本的成员。 When I delete the first method and only run the Invoke, I get a browser error stating that the automation server cant create the object.当我删除第一个方法并仅运行 Invoke 时,我收到一个浏览器错误,指出自动化服务器无法创建 object。 So is there really no way (except OOB mode) to do this?那么真的没有办法(除了OOB模式)来做到这一点吗?

Yes, the explanation is correct: you should add at least one method with the ScriptableMember attribute in order that you can use the RegisterScriptableObject method.是的,解释是正确的:您应该添加至少一个具有ScriptableMember属性的方法,以便您可以使用RegisterScriptableObject方法。 But it is used only for calling C#-methods from JavaScript.但它仅用于从 JavaScript 调用 C# 方法。

As far as I see, you want to do the opposite: to call JavaScript code from the Silverlight application.据我所知,您想做相反的事情:从 Silverlight 应用程序调用 JavaScript 代码。 Then you need only one line:那么你只需要一行:

HtmlPage.Window.Invoke("RunShellCommand");

The error automation server cant create the object has nothing to do with Silverlight.错误automation server cant create the object与 Silverlight 无关。 I'm sure that if you call the JS function directly - the error will remain.我敢肯定,如果您直接调用 JS function - 错误将仍然存在。 According to the internet, the reason might be not installed Microsoft Windows Script .根据互联网,原因可能是没有安装Microsoft Windows Script Or it is because of security restrictions of the browser.或者是因为浏览器的安全限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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