. JS reference
HTML
<button onclick="window.external.Test('called from script code')">
call client code from script code
</button>
C code in WPF Code:
public void Test(String message)
{
MessageBox.Show(message, "client code");
}
Communication between WPF and JavaScript requires full trust, so you need to add the following code:
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[ComVisible(true)]
public class MainWindow
{
public void Test(String message)
{
MessageBox.Show(message, "client code");
}
}
I do that a lot
//webbrowser sets a context for script callbacks
mywebbrowser.objectForScription = nwe scriptCallBackContext();
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[ComVisible(true)]
public calss scriptCallBackContext
{
public void Test(String message)
{
MessageBox.Show(message, "client code");
}
}
c#Tune ate JS method
private void Window_Loaded(object sender, RoutedEventArgs e)
{
string curdir = Directory.GetCurrentDirectory();
webBrowser.Navigate(String.Format("file:///{0}/test.html", curdir));
webBrowser.LoadCompleted += (ss, ee) =>
{
var jsCode = "fillData('data...');";
dynamic doc = webBrowser.Document;
webBrowser.InvokeScript("execScript", new Object[] { jsCode, "JavaScript" });
};
}
myscripts.js:
function fillData(data)
{
//document.getElementById("uname").value = data;
var oVDiv = document.getElementById("uname");
//oVDiv.setAttribute("vaue", data);
oVDiv.value = data;
//oVDiv.value = data;
//document.write(data);
}
Similar Posts:
- Document.body.clientheight cannot get the browser page height correctly
- [Solved] Java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double
- ”Cannot read property ‘addEventListener’ of null“
- Eclipse Error:The selection cannot be launched, and there are no recent launches
- [Solved] Error in v-on handler: “TypeError: this.$confirm is not a function“
- [Solved] Java Call Error: java.lang.IllegalArgumentException: wrong number of arguments
- JavaScript Uncaught TypeError: Cannot set property ‘innerHTML’ of null”
- [How to Solve] Cannot set property ‘onclick’ of null
- Java Thread wait, notify and notifyAll Example
- How to Solve Error: Cannot set property ‘onclick’ of null