导航:首页 > 编程语言 > winform与js交互

winform与js交互

发布时间:2021-12-05 10:35:14

A. C#中webbrowser与javascript(js)交互,使程序集COM可见”。这样做会爆漏程序的安全性吗

开发环境是Visual Studio 2008 .阅读此文需要有C#和javascript开发基础。

1.首先新建一个项目,在默认窗体form1上拖拽一个webbrowser1.

做完这些基本的设置之后就可以让webbrowser和js交互了,交互方法如下:

C#调用js函数的方法:

首先在js中定义被c#调用的方法:
function Messageaa(message)
{
alert(message);
}
在c#调用js方法Messageaaprivate void button1_Click(object sender, EventArgs e)
{ // 调用JavaScript的messageBox方法,并传入参数
object[] objects = new object[1];
objects[0] = "c#diao javascript";
webBrowser1.Document.InvokeScript("Messageaa", objects);
}

用JS调用C#函数的方法:

首先在c#中定义被js调用的方法:public void MyMessageBox(string message)
{
MessageBox.Show(message);
}

在js中调用c#方法:<!-- 调用C#方法 -->
<button onclick="window.external.MyMessageBox('javascript访问C#代码')" >javascript访问C#代码</button>

B. winform 中如何引用js和css

  1. 在使用#开发winform程序过程中,经常会碰到嵌入了一个WebBrowser的浏览器控件。很多时候,需要在程序里控制网页的显示方式,或者调用网页当中的某个JS函数,反过来,也有可能网页也需要调用程序当中的函数来实现某些功能。下面讲解下如何互相进行交互。

  2. 程序调用JS脚本如下:

  3. <scriptlanguage=”javascript”>
    functionShopXG(infor)
    {
    alert(‘我要开网店、携购独立网店系统,模板最全,最专业的系统!’);
    return;
    }
    </script>
  4. C#代码调用如下:
    using System.Security.Permissions;
    注意: 类定义前需要加上下面两行,否则调用失败!
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]

    调用的函数:
    webBrowser1.Document.InvokeScript("ShopXG ", new string[] {‘ssss’});

  5. JS中调用C#函数示例:
    C#函数如下:
    public void ShowMessage(string message)
    {
    MessageBox.Show(“免费开网店,携购独立网店系统诚邀您的加盟!”);
    }

  6. JS里的调用方法:
    <script language=”Javascript”>
    functionInvokeFunc()
    {
    window.external.ShowMessage(‘呵呵’);
    }
    </script>

C. C# WinForm程序如何与js交互

程序调用JS脚本如下: JS脚本代码: function ShopXG(infor){alert(‘我要开网店、携购独立网店系统,模板最全,最专业的系统!’);return;}C#代码调用如下: using System.Security.Permissions; 注意: 类定义前需要加上下面两行,否则调用失败! [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] [System.Runtime.InteropServices.ComVisibleAttribute(true)] 调用的函数: webBrowser1.Document.InvokeScript("ShopXG ", new string[] {‘ssss’}); JS中调用C#函数示例: C#函数如下: public void ShowMessage(string message){MessageBox.Show(“免费开网店,携购独立网店系统诚邀您的加盟!”);}JS里的调用方法: function InvokeFunc(){window.external.ShowMessage(‘呵呵’);}在使用C#开发winform程序过程中,我们经常会碰到嵌入了一个WebBrowser的浏览器控件。很多时候,我们需要在程序里控制网页的显示方式,或者调用网页当中的某个JS函数,反过来,也有可能网页也需要调用程序当中的函数来实现某些功能。下面我给大家讲解下如何互相进行交互。 程序调用JS脚本如下: JS脚本代码: function ShopXG(infor){alert(‘我要开网店、携购独立网店系统,模板最全,最专业的系统!’);return;}C#代码调用如下: using System.Security.Permissions; 注意: 类定义前需要加上下面两行,否则调用失败! [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] [System.Runtime.InteropServices.ComVisibleAttribute(true)] 调用的函数: webBrowser1.Document.InvokeScript("ShopXG ", new string[] {‘ssss’}); JS中调用C#函数示例: C#函数如下: ”);}JS里的调用方法:

D. C#和JavaScript

既然你用C#语言,那么用的是asp.net进行网站编程了,而asp.net的前台脚本就是javascript,后台支持语言就C#,两者不是运用哪个,而是必须都用。javascript的运用是为了可以是页面动态刷新验证,可以使程序的负担减小,同时减少对数据库的访问减轻数据库的压力。再者,前台javascript的运用还减少了页面间数据的传输,使加载速度更快,不然你的网页打开和刷新会慢的像乌龟。

E. C# winform调用js文件中的方法

1、js文件中的语言属于javascript,而javascript是运行在网页上,也就是在浏览器端运行的;
2、程专序分为cs架构属和bs架构。
cs:Client/Server或客户/服务器模式。服务器通常采用高性能的PC、工作站或小型机,并采用大型数据库系统,如Oracle、Sybase、Informix或 SQL Server。客户端需要安装专用的客户端软件。

bs:Brower/Server的缩写,客户机上只要安装一个浏览器(Browser),如Netscape Navigator或Internet Explorer,服务器安装Oracle、Sybase、Informix或 SQL Server等数据库。浏览器通过Web Server 同数据库进行数据交互。

winform程序属于cs架构,无法调用js文件!

F. C#后台代码,用JS怎么和前台交互。

Page.ClientScript.RegisterStartupScript(this.GetType(), “”, "这里面填写你的js方法", true);
如:
ClientScript.RegisterStartupScript(this.GetType(), “”, "alert('这是后台调用js')", true);

其中参数中的true表示自己在你的方法两端加"<script>"标签,如果设置为false则要自己加上<script>标签.

G. 如何用c#本地代码实现与Webbrowser中的JavaScript交互

[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public class Form1 : Form
{
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.AllowWebBrowserDrop = false;
webBrowser1. = false;
webBrowser1.WebBrowserShortcutsEnabled = false;
webBrowser1.ObjectForScripting = this;
// Uncomment the following line when you are finished debugging.
//webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.DocumentText =
"<html><head><script>" +
"function test(message) { alert(message); }" +
"</script></head><body><button " +
"onclick=\"window.external.Test('called from script code')\">" +
"call client code from script code</button>" +
"</body></html>";
}
public void Test(String message)
{
MessageBox.Show(message, "client code");
}
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Document.InvokeScript("test",
new String[] { "called from client code" });
}
}
链接0:codeproject中VB和js的交互
链接1:自定义数据类型的参数传递
代码:
dynamic data = webBrowser1.Document.InvokeScript("eval", new[] {
"(function() { return { latitude: 1, longitude: 2 }; })()" });
MessageBox.Show("Data: " + data.latitude + ", " + data.longitude);
链接:添加js到已加载的网页
代码:
private void addScript(HtmlElement head, string scriptSource)
{
HtmlElement lhe_script = head.Document.CreateElement("script");
IHTMLScriptElement script = (IHTMLScriptElement)lhe_script.DomElement;
script.src = scriptSource;
head.AppendChild(lhe_script);
}
addScript(Webbrowser.Head, @"<Change File Path here>jquery.min.js");
addScript(WebBrowser.Head, @"InjectMonitor.js");
Selenium则是一个利用http协议,来实现js和其他语言之间的通信,他强大的地方是js部分。
ide/main/src/content/selenium-runner.js
// overide _executeCurrentCommand so we can collect stats of the commands executed
_executeCurrentCommand : function() {
/**
* Execute the current command.
*
* @return a function which will be used to determine when
* execution can continue, or null if we can continue immediately
*/
var command = this.currentCommand;
LOG.info("Executing: |" + command.command + " | " + command.target + " | " + command.value + " |");
var handler = this.commandFactory.getCommandHandler(command.command);
if (handler == null) {
throw new SeleniumError("Unknown command: '" + command.command + "'");
}
command.target = selenium.preprocessParameter(command.target);
command.value = selenium.preprocessParameter(command.value);
LOG.debug("Command found, going to execute " + command.command);
updateStats(command.command);
this.result = handler.execute(selenium, command);
this.waitForCondition = this.result.terminationCondition;
},
selenium-api,CommandHandlerFactory是Api核心,在selenium-api.js,selenium-commandhandlers.js文件中实现。

H. C#中可以嵌入js编译器吗这样我就好在.net中用js来写代码了

可以

解决方案资源管理器窗口 -> 右击引用 -> 选择COM中的Mircosoft Script Control -> 确定。这样会将程序集MSScriptControl添加在引用中

privatevoidbutton1_Click(objectsender,EventArgse)
{
MSScriptControl.ScriptControlClasssc=newMSScriptControl.ScriptControlClass();
sc.Language="javascript";
//Eval参数传JS代码
sc.Eval("functionadd(a,b){returna+b;}");
objectresult=sc.Run("add",newobject[]{1,2});
MessageBox.Show(result.ToString());
}

I. c#与javascript的大量数据交互

如果是页面加载时候就传到前台,在前台可以使用
var sReturn = "<%=backStringContent %>";
如果是加载完成之后版交换数据,得权用ajax了,具体例子可以查看
http://zzg.javaeye.com/blog/113041
这个例子,希望能帮助你

J. 在UWP中怎么通过WebView控件进行C#与JS的交互

本文收集于网络,只用于方便查找方案,感谢源作者,如果侵权请联系删除

在UWP中如何通过WebView控件进行C#与JS的交互

最近由于项目需求,需要利用C#在UWP中与JS进行交互,由于还没有什么实战经验,所有就现在网上网络了一下,但是网络的结果显示大部分都是在Android和IOS上面的方法,UWP中的几乎没有。还好微软又他强大的MSDN社区,所有就在那里面找到了一个解题思路,于是就分享给大家。
1、首先,我们既然要与JS进行交互,那必须先有一个HTML页面才可以。我这里以一个本地的HTML页面作为一个HTML页面(别忘了里面要有JS部分哦( ╯□╰ ) ,并添加到当前的UWP工程中),示例代码如下所示:
1 <!DOCTYPE html>
2 <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <meta charset="utf-8" />
5 <title>Matt's Webview Content Page</title>
6 <script lang="en-us" type="text/javascript">
7 function TimeUpdate() {
8 var TimeTextbox = document.getElementById("TheTime");
9 TimeTextbox.value = new Date().toTimeString();
10 }
11 </script>
12 </head>
13 <body>
14 <h2>Matt's Webview Content Page</h2>
15 <h5>The current time is: <input type="text" id="TheTime" /> </h5>
16 <button onclick="TimeUpdate()">Update the time!</button>
17 </body>
18 </html>

上面这串HTML页面的功能很简单,就是直接获取当前时间并显示出来;
2、接下来,我们需要在在当前工程中进行前台的布局,布局很简单,这里就直接列出示例代码:
1 <Grid Background="{StaticResource }">
2 <StackPanel>
3 <WebView x:Name="MyWebview" Width="500" Height="500" Source="ms-appx-web:///HTMLPage1.html"/>
4 <Button x:Name="MyButton" Margin="10" Content="Invoke the TimeUpdate Javascript function from C# using this button" Click="MyButton_Click_1" HorizontalAlignment="Center"/>
5 </StackPanel>

WebView控件用于显示本地的一个HTML页面,Button控件用于与HTML中的JS进行交互(其实让这个按钮也可以触发HTML中获取当前时间并显示的JS事件)
3、添加Button对应的事件,添加后台代码:
1 private async void MyButton_Click_1(object sender, RoutedEventArgs e)
2 {
3 await MyWebview.InvokeScriptAsync("TimeUpdate", null); //第一个参数是要触发的JS函数,第二个参数是要传递给该函数的参数
4 }

通过上面的三个步骤,我们就可以利用这个Button按钮获取当前时间并显示在WebView中。

阅读全文

与winform与js交互相关的资料

热点内容
大脸app安卓下载 浏览:439
怎么休改文件名 浏览:989
cdr导出图片不显示文件名 浏览:761
pcdmis如何离线编程 浏览:201
微信推文插入文件 浏览:844
生产文件柜图片 浏览:244
如何设置微信数据增加 浏览:95
excel文件不再增大 浏览:613
ug编程刀路怎么复制不用鼠标选取 浏览:97
excel文件打开扩展名错误 浏览:10
阿里巴巴怎么做数据包 浏览:442
无线网络连接一直显示未连接 浏览:25
windows7如何自动激活工具下载 浏览:57
天语救砖工具 浏览:839
网络体育课程有哪些 浏览:19
苹果微信怎么查看文件 浏览:165
安卓nba2k14面补替换 浏览:450
路由器密码定时换 浏览:626
aicc恢复文件在哪里 浏览:167
怎么查无线网络属性 浏览:874

友情链接