2014-11-03 72 views
2

我想为Windows Phone开一个笑话应用程序。该应用程序从互联网上得到笑话,所以我希望它能够检查WiFi信号或能够连接到互联网。我将如何去创建它?Windows phone 8,检查wifi是否打开?

+1

ping一个应该/像谷歌的公共dns一样可用的ip吗?拥有wifi没有任何意义。就好像在说:“我有一杯,为什么它里面没有啤酒?” – 2014-11-03 21:23:19

回答

2

在WP8.1运行,您可以查询大多数的这些东西从NetworkInformation类,像这样:

// need this namespace 
using Windows.Networking.Connectivity; 

bool is_wifi_connected = false; 
ConnectionProfile current_connection_for_internet = NetworkInformation.GetInternetConnectionProfile(); 
if (current_connection_for_internet.IsWlanConnectionProfile) 
{   
    if (current_connection_for_internet.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess) 
    { 
     is_wifi_connected = true; 
    }   
} 

WP8.0可以查询大多数的这些东西从NetworkInterfaceList像这样:

// see is wifi is ON and connected 
using Microsoft.Phone.Net.NetworkInformation; 

bool is_wifi_connected = false; 
if (NetworkInterface.GetIsNetworkAvailable()) 
{ 
    NetworkInterfaceList nif = new NetworkInterfaceList(); 
    foreach(NetworkInterfaceInfo item in nif) 
    { 
     if (item.InterfaceSubtype == NetworkInterfaceSubType.WiFi && item.InterfaceState == ConnectState.Connected) 
     { 
      is_wifi_connected = true; 
     } 
    } 
} 

我们看看它是否可以连接到该网站只是打开一个连接。你可以使用任何你喜欢的网络连接。当文件完成下载时,只需要使用WebClient和事件处理程序来完成此操作。

using System.Net; 

// check network and is_wifi_connected 
if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable() && is_wifi_connected) 
{ 
    WebClient downloader = new WebClient(); 
    Uri uri = new Uri("http://www.google.com", UriKind.Absolute); 
    downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadDone); 
    downloader.DownloadStringAsync(uri); 
} 

void DownloadDone(object sender, DownloadStringCompletedEventArgs e) 
{ 
    if (e.Result == null || e.Error != null) 
    { 
     MessageBox.Show("There was an error downloading the site."); 
    } 
    else 
    { 
     // your html file or what not is stored here 
     string content = e.Result; 
    } 
} 
+0

感谢您的回复,它说“无法找到NetworkInterfaceList”...,我如何使这个工作在Windows 8.1上? – Hunt3R 2014-11-04 20:03:08

+0

@ Hunt3R你的问题被标记为Windows Phone 8.0,我给你提供了解决方案。你说你需要它的Windows 8.1?你不是说Windows Phone 8.1吗?即使你说我需要Windows Phone 8.1,它仍然不清楚。重新标记您的问题或明确声明您正在使用Windows Phone 8.1(运行时)或Windows Phone 8.1(Silverlight) - 它们是不同的东西。 – 2014-11-04 20:22:25

+0

对于windows phone 8.1,我得到一个错误sayung“NetworkInterfaceList无法找到”。那是问题和另一个问题是我怎么能在Windows 8.1中做同样的事情? – Hunt3R 2014-11-04 20:37:58