Sono nuovo a questo e sto creando una semplice applicazione in cui faccio clic su un pulsante e una notifica sul desktop dovrebbe essere visualizzato. Sto facendo questo in windows form c#
notifica desktop utilizzando javascript in forma di finestra
l'errore è "NullReferenceException was unhandled
ho un pulsante Notify
in form1 Ho provato questo:.
Form1.cs
public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
webBrowser1.ScriptErrorsSuppressed = true;
}
private void btnNotify_Click(object sender, EventArgs e)
{
webBrowser1.Document.InvokeScript("notifyMe");
}
private void Form1_Load(object sender, EventArgs e)
{
string CurrentDirectory = Directory.GetCurrentDirectory();
webBrowser1.Navigate(Path.Combine(CurrentDirectory,"HTMLPage1.html"));
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.ObjectForScripting = this;
di codice per HTMLPage1. html:
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<script language="javascript" type="text/javascript">
document.addEventListener('DOMContentLoaded', function() {
if (Notification.permission !== "granted")
Notification.requestPermission();
});
function notifyMe() {
if (!Notification) {
alert('Desktop notifications not available in your browser. Try Chromium.');
return;
}
if (Notification.permission !== "granted")
Notification.requestPermission();
else {
var notification = new Notification('Notification title', {
icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
body: "Hey there! You've been notified!",
});
notification.onclick = function() {
window.open("http://stackoverflow.com/a/13328397/1269037");
};
}
}
</script>
</head>
<body>
</body>
</html>
anche se metto semplicemente alert("Hi")
nella funzione notifyMe(), nient'altro. mostra ancora lo stesso errore.
Dove esattamente si ottiene la NullReferenceException si prega di inviare la traccia dello stack – Christoph
private void btnNotify_Click (object sender, EventArgs e) { webBrowser1.Document.InvokeScript' ("NotifyMe") '; }?. @christoph –
bisogno di aiuto al più presto –