navigator.connection不会在HTML5中返回值

时间:2014-04-01 06:14:07

标签: c# javascript html html5 windows-phone-8

我在使用javascript函数中的navigator.connection获取值时遇到问题。我有两个函数,我试图返回数据,并抛出错误说System.SystemException。

//Connection Type
    function getConnectionType() {
        //var connectionType = navigator.connection;
        var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
        var connectionType = connection.type

        window.external.notify("COT" + connectionType.toString());
    }

//Bandwidth
function getBandwidth() {
        //var online = navigator.onLine;
        var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
        var bandwidth = connection.bandwidth

        //window.external.notify("BAN" + bandwidth.toString() + " Mbps");
        window.external.notify("BAN" + bandwidth.toFixed(2) + " Mbps");
    }

我在C#

中使用InvokeScript调用此函数
private void RunPerformanceTestButton_Click(object sender, RoutedEventArgs e)
    {
        //Bandwidth
        object bandwidth = Browser.InvokeScript("getBandwidth"); //Error

        //Connection Type
        object connectionType = Browser.InvokeScript("getConnectionType"); //Error
    }

private void Browser_ScriptNotify(object sender, NotifyEventArgs e)
    {
        string value = null;
        string statistic = null;

        statistic = e.Value.Substring(0, 3);
        value = e.Value.Substring(3);     

        switch (statistic)
        {
            //Bandwidth
            case "BAN":
                BANResultTextBlock.Text = value;
                break;
            //Connection type
            case "COT":
                COTResultTextBlock.Text = value;
                break;
            default:
                return;
        }
    }

错误

  

System.SystemException未被用户代码
处理   HResult = -2146233087消息=发生了未知错误。错误:   80020101. Source = Microsoft.Phone.Interop StackTrace:          在Microsoft.Phone.Controls.NativeMethods.ValidateHResult(Int32 hr)          在Microsoft.Phone.Controls.WebBrowserInterop.InvokeScript(String   scriptName,String [] args)          在Microsoft.Phone.Controls.WebBrowser.InvokeScript(String scriptName)          在Network_Performance_Test.MainPage.RunPerformanceTestButton_Click(对象   发件人,RoutedEventArgs e)          在System.Windows.Controls.Primitives.ButtonBase.OnClick()          在System.Windows.Controls.Button.OnClick()          在System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs)   E)          在System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl,EventArgs e)          在MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj,IntPtr unmanagedObjArgs,Int32 argsTypeIndex,Int32 actualArgsTypeIndex,   String eventName)InnerException:

我认为问题出在navigation.connection上。我使用相同的实现来返回window.performance.timing数据并且它完美地运行。有任何想法吗?我真的需要这些数据用于性能和增强目的。需要注意的是,这都是在Windows Phone 8中的WebBrowser控件中完成的。

1 个答案:

答案 0 :(得分:0)

引用http://www.sitepoint.com/use-network-information-api-improve-responsive-websites/表示At the time of writing, the Network Information API has little browser support and could change.