如何检查Windows 8存储HTML / JS应用程序中的多个页面是否可以使用Internet连接?

时间:2012-11-13 17:08:28

标签: windows-8 winjs

在主页中,我使用了registerForNetworkStatusChangeNotif()函数(which I borrowed from MSDN)来注册网络通知更改。该函数添加了一个事件监听器networkstatuschanged,它可以单独用于该页面,即连接中断时显示错误,并且在恢复连接时自动刷新页面 -

var networkInfo = Windows.Networking.Connectivity.NetworkInformation; ... networkInfo.addEventListener(" networkstatuschanged",onNetworkStatusChange);

我应该如何处理其他网页的互联网可用性检查?

我已经尝试在default.js中注册此功能,以便它可用于所有页面。我的应用程序使用Geolocation功能和statuschanged事件与networkstatuschanged事件冲突,我的应用程序无法显示Internet连接错误的丢失。如何解决此问题,以便正确处理Internet连接的不可用性。

2 个答案:

答案 0 :(得分:4)

您需要收听特定事件。当它发射时,你会检查互联网连接。

var networkInformation = Windows.Networking.Connectivity.NetworkInformation;
...

ready: function(element, options)
{
   // Registering for connection change
   networkInformation.addEventListener("networkstatuschanged", this.onNetworkStatusChanged);
   ...
}

unload: function()
{
   // Unregistering for connection change
   networkInformation.removeEventListener("networkstatuschanged", this.onNetworkStatusChanged);
   ...
}

onNetworkStatusChanged: function(eventArgs)
{
    // Retrieve the InternetConnectionProfile
    var internetConnectionProfile = networkInformation.getInternetConnectionProfile();
    // Accesses the NetworkConnectivityLevel
    var networkConnectivityLevel = internetConnectionProfile.getNetworkConnectivityLevel();
    // Switch on NetworkConnectivityLevel
    switch (networkConnectivityLevel) {
        case Windows.Networking.Connectivity.NetworkConnectivityLevel.none:
           ...
        case Windows.Networking.Connectivity.NetworkConnectivityLevel.localAccess:
           ...
        case Windows.Networking.Connectivity.NetworkConnectivityLevel.constrainedInternetAccess:
           ...
        case Windows.Networking.Connectivity.NetworkConnectivityLevel.internetAccess:
           ...
    }
    ...
}

答案 1 :(得分:2)

我希望自动页面'刷新'是由数据绑定引起的。要获得更多控制权,您需要在页面'ready'上绑定到Networking对象,然后在'unload'页面上再解除绑定。例如:

var networkInfo = Windows.Networking.Connectivity.NetworkInformation;
this.networkHandler = this._doSomething();
...
..

ready: function(element, options)
{
   networkInfo.addEventListener(this.networkHandler);
   ...
   ..
}

unload: function()
{
   networkInfo.removeEventListener(this.networkHandler);
   ...
   ..
}

_doSomething : function(event)
{
    // Examine the 'event' object and perform 'refresh' accordingly
    // Most likely a call to WinJS.Binding.processAll(...)
}

您需要的只是定义处理程序方法'_doSomething()'来检查'event'对象传递的连接细节,并相应地触发绑定/刷新逻辑。

希望这有帮助!