gpt4 book ai didi

javascript - 如何在没有 WebView 的 UWP 中使用 C# 执行 JavaScript 函数

转载 作者:行者123 更新时间:2023-11-29 15:23:09 25 4
gpt4 key购买 nike

假设我们的 C# UWP App 项目中包含一个 *.js 文件,我们希望能够执行此文件中包含的函数。

示例 JS 函数:

function myFunction(p1, p2) {
return p1 * p2; // The function returns the product of p1 and p2

示例 C# 代码:

public class SampleObject
{
public SampleObject(int a, int b)
{
var evaluated = <<< do some magic and get myFuction(a,b) here >>>
}
}

有没有其他方法可以让一些虚拟 WebView 加载我们的 JS 并从中调用 myFunction?我读过关于 Chakra 的文章,它看起来应该可以解决问题,但我不知道如何按照我想要的方式使用它。有几行示例吗?

最佳答案

  1. 我会使用 cscript.exe 来运行您的 JavaScript - 请参阅 https://technet.microsoft.com/en-us/library/bb490887.aspx有关这方面的信息。
  2. 使用 System.Diagnostics.Process 对象调用 cscript.exe
  3. 使用 ProcessStartInfo 对象将路径传递给您的 JavaScript 文件。
  4. 设置事件以捕获来自 StandardOutput 和 StandardError channel 的输出。请注意,并非 StandardError 返回的所有内容都一定是错误。为了方便起见,我为 Process 类创建了一个包装器,称为 cManagedProcess:

    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Text;
    using System.Threading;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    string sScriptPath = @"C:\temp\test.js";
    File.WriteAllText(sScriptPath, @"
    //Your JavaScript goes here!
    WScript.Echo(myFunction(1, 2));

    function myFunction(p1, p2) {
    return p1 * p2; // The function returns the product of p1 and p2
    }
    ");

    var oManagedProcess = new cManagedProcess("cscript.exe", sScriptPath);
    int iExitCode = oManagedProcess.Start();

    Console.WriteLine("iExitCode = {0}\nStandardOutput: {1}\nStandardError: {2}\n",
    iExitCode,
    oManagedProcess.StandardOutput,
    oManagedProcess.StandardError
    );

    Console.WriteLine("Press any key...");
    Console.ReadLine();
    }
    }

    public class cManagedProcess
    {
    private Process moProcess;

    public ProcessStartInfo StartInfo;

    private StringBuilder moOutputStringBuilder;
    public string StandardOutput
    {
    get
    {
    return moOutputStringBuilder.ToString();
    }
    }

    private StringBuilder moErrorStringBuilder;
    public string StandardError
    {
    get
    {
    return moErrorStringBuilder.ToString();
    }
    }

    public int TimeOutMilliSeconds = 10000;

    public bool ThrowStandardErrorExceptions = true;

    public cManagedProcess(string sFileName, string sArguments)
    {
    Instantiate(sFileName, sArguments);
    }

    public cManagedProcess(string sFileName, string sFormat, params object[] sArguments)
    {
    Instantiate(sFileName, string.Format(sFormat, sArguments));
    }

    private void Instantiate(string sFileName, string sArguments)
    {
    this.StartInfo = new ProcessStartInfo()
    {
    UseShellExecute = false,
    CreateNoWindow = true,
    RedirectStandardError = true,
    RedirectStandardOutput = true,
    FileName = sFileName,
    Arguments = sArguments
    };
    }

    private AutoResetEvent moOutputWaitHandle;
    private AutoResetEvent moErrorWaitHandle;

    /// <summary>
    /// Method to start the process and wait for it to terminate
    /// </summary>
    /// <returns>Exit Code</returns>
    public int Start()
    {
    moProcess = new Process();
    moProcess.StartInfo = this.StartInfo;
    moProcess.OutputDataReceived += cManagedProcess_OutputDataReceived;
    moProcess.ErrorDataReceived += cManagedProcess_ErrorDataReceived;

    moOutputWaitHandle = new AutoResetEvent(false);
    moOutputStringBuilder = new StringBuilder();

    moErrorWaitHandle = new AutoResetEvent(false);
    moErrorStringBuilder = new StringBuilder();

    bool bResourceIsStarted = moProcess.Start();

    moProcess.BeginOutputReadLine();
    moProcess.BeginErrorReadLine();

    if (
    moProcess.WaitForExit(TimeOutMilliSeconds)
    && moOutputWaitHandle.WaitOne(TimeOutMilliSeconds)
    && moErrorWaitHandle.WaitOne(TimeOutMilliSeconds)
    )
    {
    if (mbStopping)
    {
    return 0;
    }

    if (moProcess.ExitCode != 0 && ThrowStandardErrorExceptions)
    {
    throw new Exception(this.StandardError);
    }
    return moProcess.ExitCode;
    }
    else
    {
    throw new TimeoutException(string.Format("Timeout exceeded waiting for {0}", moProcess.StartInfo.FileName));
    }
    }

    private bool mbStopping = false;
    public void Stop()
    {
    mbStopping = true;
    moProcess.Close();
    }


    private void cManagedProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
    DataRecieved(e, moOutputWaitHandle, moOutputStringBuilder);
    }

    private void cManagedProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e)
    {
    DataRecieved(e, moErrorWaitHandle, moErrorStringBuilder);
    }

    private void DataRecieved(DataReceivedEventArgs e, AutoResetEvent oAutoResetEvent, StringBuilder oStringBuilder)
    {
    if (e.Data == null)
    {
    oAutoResetEvent.Set();
    }
    else
    {
    oStringBuilder.AppendLine(e.Data);
    }
    }
    }
    }

关于javascript - 如何在没有 WebView 的 UWP 中使用 C# 执行 JavaScript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41831800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com