Nasıl yapılır: DHTML Koduyla İstemci Uygulaması Kodu Arasında İki Yönlü İletişim Gerçekleştirme
bu WebBrowser denetimi, Windows Forms istemci uygulamalarınıza var olan dinamik HTML (DHTML) Web uygulaması kodu eklemek için kullanabilirsiniz. bu, DHTML tabanlı denetimler oluşturma konusunda önemli bir geliştirme süresi yatırdığınızda ve mevcut kodu yeniden yazmak zorunda kalmadan Windows Forms zengin kullanıcı arabirimi özellikleri avantajlarından yararlanmak istediğinizde yararlıdır.
WebBrowserDenetim, ve özellikleri aracılığıyla istemci uygulama kodunuz Ile Web sayfanızın betik oluşturma kodu arasında iki yönlü iletişim uygulamanıza olanak sağlar ObjectForScriptingDocument . Ayrıca, WebBrowser denetimi, Web denetimlerinizin uygulama formunuzdaki diğer denetimlerle sorunsuz bir şekilde karışmasını sağlayacak şekilde, DHTML uygulamasını gizleyerek yapılandırabilirsiniz. Denetimleri sorunsuzca karıştırmak için, sayfanın arka plan rengi ve görsel stilinin formun geri kalanıyla eşleşmesi için görüntülenen sayfayı biçimlendirin ve AllowWebBrowserDropIsWebBrowserContextMenuEnabledWebBrowserShortcutsEnabled standart tarayıcı özelliklerini devre dışı bırakmak için, ve özelliklerini kullanın.
Windows Forms uygulamanıza DHTML eklemek için
WebBrowserAllowWebBrowserDrop
falseWebBrowser Denetimin üzerinde bırakılan dosyaları açmasını engellemek için denetimin özelliğini olarak ayarlayın.webBrowser1.AllowWebBrowserDrop = false;webBrowser1.AllowWebBrowserDrop = FalseIsWebBrowserContextMenuEnabled
falseWebBrowser Kullanıcı sağ tıkladığında denetimin kısayol menüsünü görüntülemesini engellemek için denetimin özelliğini olarak ayarlayın.webBrowser1.IsWebBrowserContextMenuEnabled = false;webBrowser1.IsWebBrowserContextMenuEnabled = FalseWebBrowserShortcutsEnabled
falseWebBrowser Denetimin kısayol tuşlarına yanıt vermesini engellemek için denetimin özelliğini olarak ayarlayın.webBrowser1.WebBrowserShortcutsEnabled = false;webBrowser1.WebBrowserShortcutsEnabled = FalseObjectForScriptingFormun oluşturucusunda özelliğini ayarlayın veya yöntemini geçersiz kılın OnLoad .
Aşağıdaki kod, komut dosyası nesnesi için form sınıfının kendisini kullanır.
webBrowser1.ObjectForScripting = new MyScriptObject(this);webBrowser1.ObjectForScripting = New MyScriptObject(Me)Komut dosyası nesneniz uygulayın.
public class MyScriptObject { private Form1 _form; public MyScriptObject(Form1 form) { _form = form; } public void Test(string message) { MessageBox.Show(message, "client code"); } }Public Class MyScriptObject Private _form As Form1 Public Sub New(ByVal form As Form1) _form = form End Sub Public Sub Test(ByVal message As String) MessageBox.Show(message, "client code") End Sub End Classwindow.externalBelirtilen nesnenin ortak özelliklerine ve yöntemlerine erişmek için komut dosyası kodunuzda nesnesini kullanın.Aşağıdaki HTML kodu, bir düğme tıklaağından betik nesnesi üzerinde bir yöntemin nasıl çağrılacağını gösterir. Bu kodu, denetimin yöntemini kullanarak yüklediğiniz Navigate veya denetimin özelliğine atadığınız BIR HTML BELGESININ Body öğesine kopyalayın DocumentText .
<button onclick="window.external.Test('called from script code')"> call client code from script code </button>Betik kodunuzda, uygulama kodunuzun kullanacağı işlevleri uygulayın.
Aşağıdaki HTML BETIĞI öğesi örnek bir işlev sağlar. Bu kodu, denetimin yöntemini kullanarak yüklediğiniz Navigate veya denetimin özelliğine atadığınız BIR HTML BELGESININ baş öğesine kopyalayın DocumentText .
<script> function test(message) { alert(message); } </script>Documentİstemci uygulama kodunuzda betik koduna erişmek için özelliğini kullanın.
Örneğin, bir düğme olay işleyicisine aşağıdaki kodu ekleyin Click .
webBrowser1.Document.InvokeScript("test", new String[] { "called from client code" });webBrowser1.Document.InvokeScript("test", _ New String() {"called from client code"})DHTML kodunuzda hata ayıklamayı tamamladıktan sonra, ScriptErrorsSuppressed
trueWebBrowser denetimin komut dosyası kodu sorunları için hata iletilerini görüntülemesini engellemek için denetimin özelliğini olarak ayarlayın.// Uncomment the following line when you are finished debugging. //webBrowser1.ScriptErrorsSuppressed = true;' Uncomment the following line when you are finished debugging. 'webBrowser1.ScriptErrorsSuppressed = True
Örnek
Aşağıdaki kod örneği, bu özelliği anlamak için kullanabileceğiniz bir tanıtım uygulaması sağlar. HTML kodu, WebBrowserDocumentText ayrı bir HTML dosyasından yüklenmesi yerine özelliği aracılığıyla denetime yüklenir.
using System;
using System.Windows.Forms;
public class Form1 : Form
{
private WebBrowser webBrowser1 = new WebBrowser();
private Button button1 = new Button();
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
public Form1()
{
button1.Text = "call script code from client code";
button1.Dock = DockStyle.Top;
button1.Click += new EventHandler(button1_Click);
webBrowser1.Dock = DockStyle.Fill;
Controls.Add(webBrowser1);
Controls.Add(button1);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
webBrowser1.AllowWebBrowserDrop = false;
webBrowser1.IsWebBrowserContextMenuEnabled = false;
webBrowser1.WebBrowserShortcutsEnabled = false;
webBrowser1.ObjectForScripting = new MyScriptObject(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>";
}
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Document.InvokeScript("test",
new String[] { "called from client code" });
}
}
public class MyScriptObject
{
private Form1 _form;
public MyScriptObject(Form1 form)
{
_form = form;
}
public void Test(string message)
{
MessageBox.Show(message, "client code");
}
}
Imports System.Windows.Forms
Public Class Form1
Inherits Form
Private webBrowser1 As New WebBrowser()
Private WithEvents button1 As New Button()
<STAThread()> _
Public Shared Sub Main()
Application.EnableVisualStyles()
Application.Run(New Form1())
End Sub
Public Sub New()
button1.Text = "call script code from client code"
button1.Dock = DockStyle.Top
webBrowser1.Dock = DockStyle.Fill
Controls.Add(webBrowser1)
Controls.Add(button1)
End Sub
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
webBrowser1.AllowWebBrowserDrop = False
webBrowser1.IsWebBrowserContextMenuEnabled = False
webBrowser1.WebBrowserShortcutsEnabled = False
webBrowser1.ObjectForScripting = New MyScriptObject(Me)
' 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>"
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles button1.Click
webBrowser1.Document.InvokeScript("test", _
New String() {"called from client code"})
End Sub
End Class
Public Class MyScriptObject
Private _form As Form1
Public Sub New(ByVal form As Form1)
_form = form
End Sub
Public Sub Test(ByVal message As String)
MessageBox.Show(message, "client code")
End Sub
End Class
Kod Derleniyor
Bu kod şunları gerektirir:
- Sisteme ve sisteme başvurular. Windows. Forms derlemeleri.