如何在不使用windows.script.Notify()的情况下将多个参数从javascript传递到windowsp

How to pass multiple parameters from javascript to windows phone methods without using windows.script.Notify()?

本文关键字:参数 javascript windowsp 情况下 windows Notify script      更新时间:2024-05-16

这里我想将多个参数从javascript传递到windowsphone方法。我需要在script_notify完成后执行此操作。

以下是C#-Windows手机代码:

private void Browser_ScriptNotify(object sender, NotifyEventArgs e)
    {
        flyersInfos = "[{'"app_user_id'": '"" + thisapp.AppId + "'",'"flyer_user_id'": '"" + thisapp.FlyersId + "'",'"device_id'": '"" + thisapp.DeviceId + "'",'"device_info'": '"" + thisapp.DeviceInfo + "'",'"Latitude'": '"" + thisapp.Latitude + "'",'"Longitude'": '"" + thisapp.Longitude + "'"}]";
        if (e.Value == "getMemoryUsage")
            Browser.InvokeScript("renderForm", flyersInfos);
        return;
    }

Onload()中的javascript Notify函数,

function onload()
{
  window.external.notify("getMemoryUsage");   
 }

快速方式:

它是一个void事件处理程序,因此您可以使其异步并延迟:

private async void Browser_ScriptNotify(...)
{
  ...
  await Task.Delay(1);
  ...
  // everything here will execute after the original ScriptNotify event
  // was completely finished, as it continues on the same (UI) thread.
}

正确的方法是使用队列或一些消息传递实现。

只能将单个参数作为stringjavascript传递给C#类。但如果您需要发送多个参数,我建议用特定的分隔符连接参数

例如,从javascript:

window.external.notify("parameter1+parameter2+parameter3");

在这里,我使用了一个字符串作为参数来发送所有三个字符串。最后解析字符串并重建C#代码中原来的三个参数。