问题描述
我正在开发一个网站以及 Windows 应用程序,我将使用相同的网页用于 Windows 窗体应用程序和网站.
I'm developing a web site as well as windows application and i'm going to use same web page for windows form application as well as for web site.
所以我想在单击放置在网页上的链接后调用 windows 窗体控件.我想将 windows 窗体控件显示为弹出窗体.
so i want to call the windows form control after clicking on the link which is placed on the web page. And i want to show the windows form control as a pop up form.
如何做到这一点?或者提供一些与此问题相关的文档.
How to do this? Or provide some document related to this issue.
推荐答案
假设您的意思是在 WinForms 应用程序中托管 WebBrowser
类,你可以提供一个对象给它的ObjectForScripting
属性.它提供了一种调用方法来触发您的 WinForms 代码.例如:
Assuming you mean that within your WinForms application you're hosting an instance of the WebBrowser
class, you can provide an object to its ObjectForScripting
property. that provides a method to call to trigger your WinForms code. For example:
public partial class MyWindowsFormsForm()
{
public MyWindowsFormsForm()
{
this.WebBrowserControl.ObjectForScripting = this;
}
public void DoSomething()
{
MyOtherForm f = new MyOtherForm();
f.Show();
}
}
然后,在您的页面中:
<script language="javascript" type="text/javascript">
function loadOtherForm()
{
if (RunningInWinFormsApplication())
{
window.external.DoSomething();
}
else
{
// Code to do something when NOT running inside the WinForms app could go here
}
}
function RunningInWinFormsApplication()
{
return (window.external.DoSomething != undefined);
}
</script>
<button onclick="loadOtherForm();">Call into WinForms app</button>
有一个明显的警告,你需要在你的网页中有代码来检查以确保 window.external.DoSomething
确实存在,所以很可能你的 onclick
(在本例中)将调用一个辅助方法,该方法要么调用 WinForms,要么在页面未托管在您的应用程序中时执行任何需要执行的操作.
There's the obvious caveat that you'll need to have code in your web page that checks to ensure that window.external.DoSomething
is actually there, so likely your onclick
(in this example) would call a helper method that either calls into WinForms, or does whatever needs doing in the event that the page isn't being hosted inside your application.
这篇关于如何在网页中打开窗体控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!