我可以在silverlight app中使用JS吗?

时间:2014-12-12 14:32:21

标签: javascript c# silverlight

我有Silverlight应用程序。例如,我想添加一些可以与SL app交互的JS脚本。例如,我想添加谷歌地图使用JS api。我可以这样做,但我必须从SL发送一些数据到JS,在地图上添加图钉,在地图上绘制图形等。

1 个答案:

答案 0 :(得分:0)

如果你使用的是Windows Phone,你不介意将xaml的那部分作为webview,你可以。

首先将webview添加到xaml

<phone:WebBrowser Name="webView" BorderThickness="0" BorderBrush="Transparent" IsScriptEnabled="True" 
                              ScriptNotify="WebBrowser_ScriptNotify" />

然后你必须将webview与load事件绑定,然后将文件保存到存储并加载你的html和js文件

webView.Loaded += WebBrowser_OnLoaded;

    private void WebBrowser_OnLoaded(object sender, RoutedEventArgs e)
    {
        SaveFilesToIsoStore();
        chatView.Navigate(new Uri("Assets/HtmlContent/index.html", UriKind.Relative));
    }

    private void SaveFilesToIsoStore()
    {
        //These files must match what is included in the application package,
        //or BinaryStream.Dispose below will throw an exception.
        string[] files = {
            "Assets/HtmlContent/index.html", 
            "Assets/HtmlContent/js/libs/jquery-1.11.0.min.js",  "Assets/HtmlContent/js/pagejs.js",  "Assets/HtmlContent/css/style.css"
        };

        IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();

        if (false == isoStore.FileExists(files[0]))
        {
            foreach (string f in files)
            {
                StreamResourceInfo sr = Application.GetResourceStream(new Uri(f, UriKind.Relative));
                using (BinaryReader br = new BinaryReader(sr.Stream))
                {
                    byte[] data = br.ReadBytes((int)sr.Stream.Length);
                    SaveToIsoStore(f, data);
                }
            }
        }
    }

    private void SaveToIsoStore(string fileName, byte[] data)
    {
        string strBaseDir = string.Empty;
        string delimStr = "/";
        char[] delimiter = delimStr.ToCharArray();
        string[] dirsPath = fileName.Split(delimiter);

        //Get the IsoStore.
        IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();

        //Re-create the directory structure.
        for (int i = 0; i < dirsPath.Length - 1; i++)
        {
            strBaseDir = System.IO.Path.Combine(strBaseDir, dirsPath[i]);
            isoStore.CreateDirectory(strBaseDir);
        }

        //Remove the existing file.
        if (isoStore.FileExists(fileName))
        {
            isoStore.DeleteFile(fileName);
        }

        //Write the file.
        using (BinaryWriter bw = new BinaryWriter(isoStore.CreateFile(fileName)))
        {
            bw.Write(data);
            bw.Close();

所以在js上你必须和c#这样说话

function sendMessageToCodeBehind(someData) {
    window.external.notify(JSON.stringify({ method: 'AddMessage', data: someData }));
}

在后面的代码中,您将从webview中重新发送消息,如下所示:

    private void WebBrowser_ScriptNotify(object sender, NotifyEventArgs e)
    {
        var example = new { method = string.Empty, data = new object() };

        var obj = JsonConvert.DeserializeAnonymousType(e.Value, example);

        switch (obj.method) {
            case "methodName":
        }
    }

然后你会把消息发回给js,就像这样

webView.InvokeScript("jsMethodName", JsonConvert.SerializeObject(new { Message = "some json message" }));