WPF Web 浏览器 C# JavaScript 互操作

发布于 2024-11-18 22:54:45 字数 888 浏览 3 评论 0原文

我正在开发一个应用程序,该应用程序大部分是 html/javascript (整个“HTML5”事物),带有 WPF 应用程序的轻外壳,以便它可以与本地资源交互。

该应用程序有一个 HTML 文件,我以编程方式导航到该文件。加载文件后,我尝试附加事件处理程序,以便可以处理 C# 中的特定事件。

当我尝试使用 mshtml 对象时,似乎我只得到空值。例如,下面是一些代码:

string initUrl = "file:///" + Path.Combine(Environment.CurrentDirectory, Path.Combine("Pages", "ComposeSurface.html"));
navBrowser.Navigate(new Uri(initUrl));

navBrowser.Navigated += (o, e2) => 
{ 
    HTMLDocumentClass documentObject = navBrowser.Document as HTMLDocumentClass;
    IHTMLElement ele = documentObject.getElementById("initButton");
    if (ele != null)
    {
        Func<IHTMLEventObj, bool> evra = (arg) => { MessageBox.Show("Hello"); return true; };
        ele.onclick = evra as object;
    }
};

然而,getElementById 似乎总是返回 null。我已经仔细检查过,元素的 id 是一致的。

我的第二个问题与处理事件有关 - 只要附加一些 C# 到它,这是否适用于来自常规 HTML 表单按钮的单击事件?

I am working on an application that is for the most part html/javascript (the whole "HTML5" thing) with a light shell of a WPF application so that it can interact with local resources.

The application has a single HTML file which I navigate to programmatically. Once the file is loaded, I am trying to attach event handlers so I can handle specific events in C#.

When I try to use the mshtml objects, it appears that I only get nulls back. For example, here is some code:

string initUrl = "file:///" + Path.Combine(Environment.CurrentDirectory, Path.Combine("Pages", "ComposeSurface.html"));
navBrowser.Navigate(new Uri(initUrl));

navBrowser.Navigated += (o, e2) => 
{ 
    HTMLDocumentClass documentObject = navBrowser.Document as HTMLDocumentClass;
    IHTMLElement ele = documentObject.getElementById("initButton");
    if (ele != null)
    {
        Func<IHTMLEventObj, bool> evra = (arg) => { MessageBox.Show("Hello"); return true; };
        ele.onclick = evra as object;
    }
};

getElementById always seems to return a null, however. I've double checked and the id of the element is consistent.

My second question has to do with handling the event - will this work for a click event from a regular HTML form button as far as attaching some C# to it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文