简体   繁体   English

使用Awesomium从html按钮运行notepad.exe

[英]Run notepad.exe from a html-button using Awesomium

I've been looking for an example on how to open a process like notepad.exe from a html-element using WPF and Awesomium 1.7.5. 我一直在寻找有关如何使用WPF和Awesomium 1.7.5从html元素中打开类似notepad.exe的进程的示例。 The idea is that clicking a html element triggers a C# method using javascript as far as I understand the Awesomium API. 据我所知,单击html元素会触发使用javascript的C#方法。 However, all examples I can find refer to an earlier version that now uses obsolete functions... 但是,我可以找到的所有示例均引用了现在使用过时功能的早期版本...

Can anyone please provide me with an example on how to execute C#-code when openNotepad() is triggered? 有人可以给我提供一个示例,说明触发openNotepad()时如何执行C#代码吗?

HTML: HTML:

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>  
</head>
<body>
    <button onclick="app.openNotepad()">This button will open Notepad</button>
</body>
</html>

C#: C#:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        webControl.Source = new Uri("index.html");
    }

    private void webControl_DocumentReady(object sender, DocumentReadyEventArgs e)
    {
        BindMethods(webControl);
    }

    private void BindMethods(IWebView _webView)
    {
        JSValue result = webControl.CreateGlobalJavascriptObject("app");
        if (result.IsObject)
        {
            JSObject appObject = result;
            appObject.Bind("openNotepad", openNotepad);
        }
    }

    private JSValue openNotepad(object obj, JavascriptMethodEventArgs jsMethodArgs)
    {
        Process.Start("notepad.exe");
        return null;
    }
}

XAML: XAML:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:awe="http://schemas.awesomium.com/winfx" x:Class="Omega.MainWindow"
        Title="Omega" Height="350" Width="525" WindowStartupLocation="CenterScreen" WindowState="Maximized" ResizeMode="NoResize" Topmost="True" WindowStyle="None">
    <Grid>

        <awe:WebControl x:Name="webControl" DocumentReady="webControl_DocumentReady" />

    </Grid>
</Window>

According to Awesomium documentation to Bind and JavascriptMethodHandler , methods of handling Javascript events has changed in version 1.7.5. 根据Awesomium BindJavascriptMethodHandler的文档,在版本1.7.5中,处理Javascript事件的方法已更改。

JavascriptMethodEventHandler now obsolete, and new Bind overload should be used. JavascriptMethodEventHandler现在已过时,应使用新的Bind重载。

Now your code could look like 现在您的代码可能看起来像

private void BindMethods(IWebView _webView)
{
    JSValue result = webControl.CreateGlobalJavascriptObject("app");
    if (result.IsObject)
    {
        JSObject appObject = result;
        appObject.Bind("openNotepad", openNotepad);
    }
}

private JSValue openNotepad(object obj, JavascriptMethodEventArgs jsMethodArgs)
{
    Process.Start("notepad.exe");
    return null;
}

Note, there is example of using new overload at the end of Bind documentation page. 注意,在绑定文档页面的末尾有使用新的重载的示例。

Also note that javascript method in your sample actually not calling app.openNotepad() method, but alert("Run Notepad"); 另请注意,示例中的javascript方法实际上并未调用app.openNotepad()方法,而是alert("Run Notepad");

WshShell = new ActiveXObject("WScript.Shell"); WshShell = new ActiveXObject(“ WScript.Shell”);

WshShell.Run("c:/windows/system32/notepad.exe", 1, false); WshShell.Run(“ c:/windows/system32/notepad.exe”,1,否);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用新的替换notepad.exe - Replacing notepad.exe with a new one 使用notepad.exe在列表视图中打开活动项目 - Opening active item in a listview with notepad.exe 如何在Windows应用程序中以隐藏模式启动notepad.exe? - How to start notepad.exe in hidden mode in windows application? 以Notepad.exe的方式打开文件的标志是什么? - Which flags to open a file the way Notepad.exe does? C# 为什么 Process.Start("notepad.exe" myFile) 正常工作,而 Process.Start("notepad++.exe" myFile) 不工作 - C# Why does Process.Start("notepad.exe" myFile) is working and Process.Start("notepad++.exe" myFile) is not working 如何获取notepad.exe的确切路径以便关联文件扩展名 - How to get the exact path of notepad.exe in order to associate a file extension System.Diagnostics.Process.Start(“ Notepad.exe”); 在实时服务器上不工作 - System.Diagnostics.Process.Start(“Notepad.exe”); is not working on live server Process.Start with UseShellExecute 在前一个关闭窗口后无法将 notepad.exe 窗口带到前台 - Process.Start with UseShellExecute fails to bring notepad.exe window to the foreground after a previous close window 如何在 C# windows 应用程序的表单加载事件中的 process.start(&quot;notepad.exe&quot;) 之前播放 windows 媒体播放器 - how to play windows media player before the process.start("notepad.exe") in form load event in C# windows app 如何从Awesomium web控件复制Html? - How to Copy Html From Awesomium web control?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM