WP7-检查手机网络

本文介绍如何使用Windows Phone 7的Microsoft.Phone.Net.NetworkInformation命名空间中的NetworkInterface类来检查手机的网络状态。通过实例展示了如何获取当前网络是否可用及网络类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://blog.youkuaiyun.com/xuzhongxuan/article/details/6252792


Microsoft.Phone.Net.NetworkInformation Namespace

http://msdn.microsoft.com/en-us/library/ff707715(v=VS.92).aspx


在空间Microsoft.Phone.Net.NetworkInformation下的NetworkInterface 类 和NetworkInterfaceType 类是Windows Phone 7手机网络信息相关的类。NetworkInterface提供了当前手机网络的一些信息,NetworkInterfaceType是一个手机网络的类型的枚举。
下面用一个小例子来使用NetworkInterface和NetworkInterfaceType来检查手机的网络状态。

 

[xhtml]  view plain copy print ?
  1. <Grid   x:Name="LayoutRoot" Background="Transparent">    
  2.         <Grid.RowDefinitions>    
  3.             <RowDefinition   Height="Auto" />    
  4.             <RowDefinition     Height="*" />    
  5.         </Grid.RowDefinitions>    
  6.         <StackPanel  x:Name="TitlePanel"  Grid.Row="0"  Margin="12,17,0,28">    
  7.             <TextBlock  x:Name="PageTitle"  Text="检查手机网络"   Margin="9,-7,0,0"   Style="{StaticResource PhoneTextTitle1Style}" mce_Style="{StaticResource PhoneTextTitle1Style}" />    
  8.         </StackPanel>    
  9.         <Grid   x:Name="ContentPanel"   Grid.Row="1"   Margin="12,0,12,0">    
  10.             <TextBox  Name="Message"   Background="Yellow"    Text="Unknown"   VerticalAlignment="Center"    HorizontalAlignment="Center" Margin="131,184,94,378" Width="231" />    
  11.             <Button Content="查看网络信息" Height="72" HorizontalAlignment="Left" Margin="103,6,0,0" Name="button1" VerticalAlignment="Top" Width="244" Click="button1_Click" />    
  12.             <TextBox Height="72" HorizontalAlignment="Left" Margin="131,271,0,0" Name="netname" Text="TextBox" VerticalAlignment="Top" Width="244" />    
  13.             <TextBlock Height="49" HorizontalAlignment="Left" Margin="9,198,0,0" Name="textBlock1" Text="网络状态:" VerticalAlignment="Top" Width="121" />    
  14.             <TextBlock HorizontalAlignment="Left" Margin="9,287,0,310" Name="textBlock2" Text="网络类型:" />    
  15.         </Grid>    
  16.     </Grid>   

[c-sharp:nogutter]  view plain copy print ?
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Net;    
  5. using System.Windows;    
  6. using System.Windows.Controls;    
  7. using System.Windows.Documents;    
  8. using System.Windows.Input;    
  9. using System.Windows.Media;    
  10. using System.Windows.Media.Animation;    
  11. using System.Windows.Shapes;    
  12. using Microsoft.Phone.Controls;    
  13. using System.Net.NetworkInformation;    
  14. using Microsoft.Phone.Net.NetworkInformation;     
  15.      
  16. namespace checknet    
  17. {    
  18.     public partial class MainPage : PhoneApplicationPage    
  19.     {    
  20.         private bool networkIsAvailable;    
  21.         private NetworkInterfaceType _currentNetworkType; //网络连接的类型    
  22.      
  23.         public MainPage()    
  24.         {    
  25.             InitializeComponent();    
  26.         }    
  27.      
  28.         private void button1_Click(object sender, RoutedEventArgs e)    
  29.         {    
  30.             networkIsAvailable = Microsoft.Phone.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();//当前网络是否可用    
  31.             _currentNetworkType = Microsoft.Phone.Net.NetworkInformation.NetworkInterface.NetworkInterfaceType;//获取当前网络的类型    
  32.      
  33.             if (networkIsAvailable)    
  34.             {    
  35.                 Message.Text = "联网状态";    
  36.                 Message.Background = new SolidColorBrush(Colors.Green);    
  37.             }    
  38.             else   
  39.             {    
  40.                 Message.Text = "断网状态";    
  41.                 Message.Background = new SolidColorBrush(Colors.Red);    
  42.             }     
  43.      
  44.             switch (_currentNetworkType)    
  45.             {    
  46.                 case NetworkInterfaceType.MobileBroadbandCdma:    
  47.                     netname.Text = "Cdma网络";    
  48.                     break;    
  49.                 case NetworkInterfaceType.MobileBroadbandGsm:    
  50.                     netname.Text = "Csm网络";    
  51.                     break;    
  52.                 case NetworkInterfaceType.Wireless80211:    
  53.                     netname.Text = "Wireless网络";    
  54.                     break;    
  55.                 case NetworkInterfaceType.Ethernet:    
  56.                     netname.Text = "Ethernet网络";    
  57.                     break;    
  58.                 case NetworkInterfaceType.None:    
  59.                     netname.Text = "网络不可用";    
  60.                     break;    
  61.                 default:    
  62.                     netname.Text = "其他的网络";    
  63.                     break;    
  64.             }           
  65.         }    
  66.     }    
  67. }   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值