Windows phone 8 学习笔记(3) 通信(转)

Windows phone 8 可利用的数据通信方式比较广泛,在硬件支持的前提下,我们可以利用WiFi、蓝牙、临近感应等多种方式。数据交互一般通过套接字来完成,我们将在本文详细的分析。

快速导航:
一、WP8套接字
二、蓝牙
三、NFC
四、获取网络状态
五、访问web的几种方式

一、WP8套接字

1)创建套接字客户端

Windows phone 8中的套接字并不支持发布服务端,我们只能利用它在手机上创建套接字客户端,我们在此例中要用套接字去访问web服务器。首先,我们定义一个SocketClient来表示套接字客户端。

[C#]

public class SocketClient

{

//缓存套接字对象以便在整个生命周期重用

Socket _socket = null;

// 信号通知对象,用于异步操作完成通知

static ManualResetEvent _clientDone = new ManualResetEvent(false);

/// <summary>

/// 为每个异步调用设置超时时间

/// </summary>

const int TIMEOUT_MILLISECONDS = 30000;

/// <summary>

/// 数据缓冲区大小

/// </summary>

const int MAX_BUFFER_SIZE = 2048 * 100;

/// <summary>

/// 关闭套接字连接和释放所有相关的资源

/// </summary>

public void Close()

{

if (_socket != null)

{

_socket.Close();

}

}

}

    这个客户端包含连接服务器、发送信息和接受信息三个方法。我们将如下代码加入SocketClient类:
连接:
[C#]

/// <summary>

/// 尝试用TCP套接字连接到指定主机端口

/// </summary>

/// <param name="hostName">主机名</param>

/// <param name="portNumber">端口号</param>

/// <returns>描述连接结果</returns>

public string Connect(string hostName, int portNumber)

{

string result = string.Empty;

//创建一个终结点,主机名、端口号

DnsEndPoint hostEntry = new DnsEndPoint(hostName, portNumber);

//创建一个基于流,TCP套接字。

_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

//套接字上下文

SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();

socketEventArg.RemoteEndPoint = hostEntry;

socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e)

{

//执行的状态信息

result = e.SocketError.ToString();

//将当前线程设置为收到信息状态,这样被阻止的线程可以继续执行

_clientDone.Set();

});

//标识当前进程为未收到信息状态

_clientDone.Reset();

// 发送一个异步连接请求

_socket.ConnectAsync(socketEventArg);

//将当前线程阻止,直到当前线程收到信息或者超时时间已到

_clientDone.WaitOne(TIMEOUT_MILLISECONDS);

return result;

}

发送消息:
[C#]

/// <summary>

/// 向连接的服务器发送信息

/// </summary>

/// <param name="data">数据正文</param>

/// <returns>反馈</returns>

public string Send(string data)

{

string response = "操作超时";

//套接字是否准备好

if (_socket != null)

{

//套接字上下文

SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();

socketEventArg.RemoteEndPoint = _socket.RemoteEndPoint;

socketEventArg.UserToken = null;

socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e)

{

response = e.SocketError.ToString();

_clientDone.Set();

});

byte[] payload = Encoding.UTF8.GetBytes(data);

socketEventArg.SetBuffer(payload, 0, payload.Length);

_clientDone.Reset();

_socket.SendAsync(socketEventArg);

_clientDone.WaitOne(TIMEOUT_MILLISECONDS);

}

else

{

response = "套接字没有准备好";

}

return response;

}

接受消息:
[C#]

/// <summary>

/// 从连接服务器接收数据

/// </summary>

/// <returns>The data received from the server</returns>

public string Receive()

{

string response = "操作超时";

if (_socket != null)

{

SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();

socketEventArg.RemoteEndPoint = _socket.RemoteEndPoint;

//设置接收数据的缓冲区

socketEventArg.SetBuffer(new Byte[MAX_BUFFER_SIZE], 0, MAX_BUFFER_SIZE);

socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e)

{

if (e.SocketError == SocketError.Success)

{

// Retrieve the data from the buffer

response = Encoding.UTF8.GetString(e.Buffer, e.Offset, e.BytesTransferred);

response = response.Trim(‘\0‘);

}

else

{

response = "错误:" + e.SocketError.ToString();

}

_clientDone.Set();

});

_clientDone.Reset();

_socket.ReceiveAsync(socketEventArg);

_clientDone.WaitOne(TIMEOUT_MILLISECONDS);

}

else

{

response = "套接字没有准备好";

}

return response;

}

2)通过套接字访问HTTP网站

现在我们有了套接字客户端,这个时候我们来做一个类似于浏览器的功能,我们模拟HTTP请求发送到web服务器,代码如下:

[XAML]

    <!--LayoutRoot 是包含所有页面内容的根网格-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel 包含应用程序的名称和页标题-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="套接字访问web" Style="{StaticResource PhoneTextNormalStyle}"/>
        </StackPanel>

        <!--ContentPanel - 在此处放置其他内容-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBox x:Name="textbox1" HorizontalAlignment="Left" Height="72" Margin="-10,-17,0,0" TextWrapping="Wrap" Text="www.hao123.com" VerticalAlignment="Top" Width="395"/>
            <Button Content="转到" HorizontalAlignment="Left" Margin="364,-17,-9,0" VerticalAlignment="Top" Click="Button_Click_1"/>
        </Grid>
        <ScrollViewer Height="649" Width="468" Margin="12,47,0,0" HorizontalAlignment="Left"  Grid.Row="1"  VerticalAlignment="Top" >
            <TextBlock ScrollViewer.HorizontalScrollBarVisibility="Visible"  ScrollViewer.VerticalScrollBarVisibility="Visible" x:Name="textblok1" Height="649" Width="468" MaxHeight="649" MaxWidth="468" />
        </ScrollViewer>
    </Grid>

[C#]

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {

            string hostName = textbox1.Text.Replace("http://", string.Empty);
            SocketClient client = new SocketClient();
            //主机,端口号
            textblok1.Text = client.Connect(hostName, 80);
            //模拟一个HTTP Get请求
            textblok1.Text = client.Send("GET http://" + hostName + " HTTP/1.0\r\nHost: " + hostName + "\r\n\r\n\r\n");
            //等待相应
            textblok1.Text = client.Receive();

        }

二、蓝牙

WP8蓝牙支持对等方应用连接,也支持其他蓝牙设备,下面我们看看如何连接到对等方应用和设备。

1)连接到对等方
[C#]
        //已经搜索到的对等方列表
        IReadOnlyList<PeerInformation> peers;

        // 开始连接到对等应用
        async void AppToApp()
        {
            // 开始查找对等项,如果有这句话,即可使自己能够被其他蓝牙设备搜索到
            PeerFinder.Start();

            peers = await PeerFinder.FindAllPeersAsync();

            if (peers.Count == 0)
            {
               // 没有发现
            }
            else
            {
                // 选择第一个对等应用
                PeerInformation selectedPeer = peers[0];
                // 连接到第一个对等方应用
                var streamSocket = await PeerFinder.ConnectAsync(selectedPeer);
            }
        }
2)连接到设备

PeerFinder.AlternateIdentities["Bluetooth:Paired"] = ""; 查找所有已配对的设备。这样连接找到的设备对应的PeerInformation.ServiceName将为空,所以我们不能通过PeerFinder.ConnectAsync(selectedPeer);的方式去连接,具体示例如下:

[C#]

        //搜寻全部蓝牙设备并连接第一个
        private async void AppToDevice()
        {
            // 搜索所有配对的设备
            PeerFinder.AlternateIdentities["Bluetooth:Paired"] = "";
            var pairedDevices = await PeerFinder.FindAllPeersAsync();

            if (pairedDevices.Count == 0)
            {
                //没有发现设备
            }
            else
            {
                // 选择第一个连接的设备,此时selectedDevice.ServiceName为空
                PeerInformation selectedDevice = pairedDevices[0];

                // 主动创建一个StreamSocket
                StreamSocket socket = new StreamSocket();
                // 第二个参数是一个RFCOMM端口号,范围是1-30
                await socket.ConnectAsync(selectedDevice.HostName, "1");
            }
        }

PeerFinder.AlternateIdentities["Bluetooth:SDP"] = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";查找使用服务发现协议 (SDP) 并通过既定 GUID 播发服务的设备

[C#]

        //搜寻特定GUID的设备
        private async void AppToDevice2()
        {
            PeerFinder.AlternateIdentities["Bluetooth:SDP"] = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
            var pairedDevices = await PeerFinder.FindAllPeersAsync();

            if (pairedDevices.Count == 0)
            {
                //没有发现设备
            }
            else
            {
                // 选择第一个连接的设备
                PeerInformation selectedDevice = pairedDevices[0];

                // 主动创建一个StreamSocket
                StreamSocket socket = new StreamSocket();
                // 这种情况下selectedDevice.ServiceName等于您指定的GUID
                await socket.ConnectAsync(selectedDevice.HostName, selectedDevice.ServiceName);
            }
        }
3)侦听连接请求
[C#]
        public Page1()
        {
            InitializeComponent();
            //侦听连接请求需要先添加事件
            Loaded += MainPage_Loaded;
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            //远程对等类异步连接时触发
            PeerFinder.ConnectionRequested += PeerFinder_ConnectionRequested;
        }
        async void PeerFinder_ConnectionRequested(object sender, ConnectionRequestedEventArgs args)
        {
            // 获取到请求连接的对等方
            var peer = args.PeerInformation;
            // 回应连接
            var streamSocket = await PeerFinder.ConnectAsync(peer);
        }
4)发送消息
[C#]
        /// <summary>
        /// 在已连接的情况下发送消息
        /// </summary>
        /// <param name="socket"></param>
        public async void Send(StreamSocket socket, string msg)
        {
            var _dataWriter = new DataWriter(socket.OutputStream);

            //写入消息的长度
            uint strLength = _dataWriter.MeasureString(msg);
            _dataWriter.WriteUInt32(strLength);

            //写入消息的内容
            _dataWriter.WriteString(msg);
            uint numBytesWritten = await _dataWriter.StoreAsync();
        }
5)接收消息
[C#]
        /// <summary>
        /// 在已连接的情况下读取消息
        /// </summary>
        /// <param name="socket"></param>
        public async Task<string> Read(StreamSocket socket)
        {
            var _dataReader = new DataReader(socket.InputStream);
            // 读取消息长度
            await _dataReader.LoadAsync(sizeof(uint));
            uint msgLength = (uint)_dataReader.ReadUInt32();
            // 读取消息的内容
            await _dataReader.LoadAsync(msgLength);
            return _dataReader.ReadString(msgLength);
        }

三、NFC

当两个设备距离较近时,可以使用近场通信技术,本文演示一个使用近场通信制作聊天软件的例子。

[XAML]

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBlock x:Name="textblock1"  Text="连接状态:未连接" HorizontalAlignment="Left" Margin="10,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top">
            </TextBlock>
            <TextBox x:Name="textbox1" HorizontalAlignment="Left" Height="72" Margin="0,27,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="386"/>
            <Button Content="发送" HorizontalAlignment="Left" Margin="369,29,-9,0" VerticalAlignment="Top" Click="Button_Click_1"/>
            <ListBox x:Name="listbox1" Margin="0,100,0,0" Grid.Row="1"/>
        </Grid>

[C#]

    public partial class MainPage : PhoneApplicationPage
    {
        ProximityDevice device;

        long typeId = -1;
        long msgId = -1;
        public MainPage()
        {
            InitializeComponent();

            device = ProximityDevice.GetDefault();
            if (device == null)
            {
                IsEnabled = false;
                textblock1.Text = "您的手机不支持NFC功能";
            }
            else
            {
                //设备进入NFC识别范围时触发
                device.DeviceArrived += device_DeviceArrived;
                //设备离开NFC识别范围时触发
                device.DeviceDeparted += device_DeviceDeparted;
            }
        }

        //设备进入NFC识别范围时触发
        void device_DeviceArrived(ProximityDevice sender)
        {
            //创建消息订阅,创建好后就可以直接发消息了
            typeId = sender.SubscribeForMessage("Windows.MyMsgType", MsgReceivedHandler);
            Dispatcher.BeginInvoke(() => textblock1.Text = "连接状态:已连接");
        }

        //设备离开NFC识别范围时触发
        void device_DeviceDeparted(ProximityDevice sender)
        {
            if (typeId != -1)
            {
                //取消消息订阅
                sender.StopSubscribingForMessage(typeId);
                typeId = -1;
            }

            Dispatcher.BeginInvoke(() => textblock1.Text = "连接状态:连接中断");
        }

        /// <summary>
        /// 接收到消息后的处理逻辑
        /// </summary>
        void MsgReceivedHandler(ProximityDevice sender, ProximityMessage message)
        {
            Dispatcher.BeginInvoke(() =>
            {
                listbox1.Items.Add(("对方:" + message.DataAsString));
            }); 
        }

        /// <summary>
        /// 发送消息按钮按下时
        /// </summary>
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (textbox1.Text != string.Empty)
            {
                listbox1.Items.Add("我:" + textbox1.Text);

                if (msgId != -1)
                    device.StopPublishingMessage(msgId);

                msgId = device.PublishMessage("Windows.MyMsgType", textbox1.Text);
            }
        }
    }

现在我们看看在模拟器中的效果,如下图。模拟器中实现NFC的模拟效果请下载:http://proximitytapper.codeplex.com

Windows phone 8 学习笔记(3) 通信(转)

四、获取网络状态

很多时候,应用可能需要利用到网络,这个时候我们需要获取一些信息,比如网络是否连接,当前是连接到WiFi还是运营商蜂窝网络。这些信息关系到您的应用是否能正常工作,或者是否处于免费网络下可执行高流量下载等操作。

1. 数据感知

数据感知功能可以用来获取网络成本信息以及流量计划,帮助用户合理使用手机流量,我们看看使用方法。

[C#]

var connectionProfile = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile();

//网络接口类型
var ianaInterfaceType = connectionProfile.NetworkAdapter.IanaInterfaceType;

//连接成本信息
var connectionCost = connectionProfile.GetConnectionCost();

//当前的网络使用成本
var networkCostType = connectionCost.NetworkCostType;
//计划流量是否快用完
var approachingDataLimit = connectionCost.ApproachingDataLimit;
//是否超过计划流量
var overDataLimit = connectionCost.OverDataLimit;
//是否漫游
var roaming = connectionCost.Roaming;
2. 网络信息获取

我们可以通过API获取网络接口状态,检查手机数据网络以及WiFi连接情况等。

[XAML]

        <!--ContentPanel - 在此处放置其他内容-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <TextBlock x:Name="textblock1" HorizontalAlignment="Left" Margin="157,10,0,0" TextWrapping="Wrap" Text="未知" VerticalAlignment="Top"/>
        <TextBlock HorizontalAlignment="Left" Margin="17,10,0,0" TextWrapping="Wrap" Text="移动运营商:" VerticalAlignment="Top"/>


        <TextBlock x:Name="textblock2" HorizontalAlignment="Left" Margin="154,50,0,0" TextWrapping="Wrap" Text="未知" VerticalAlignment="Top" Height="220" Width="292"/>
        <TextBlock HorizontalAlignment="Left" Margin="14,50,0,0" TextWrapping="Wrap" Text="网络信息:" VerticalAlignment="Top"/>
            <TextBlock x:Name="textblock3" HorizontalAlignment="Left" Margin="91,275,0,0" TextWrapping="Wrap" Text="未知" VerticalAlignment="Top" Height="182" Width="365"/>
            <TextBlock HorizontalAlignment="Left" Margin="17,275,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="106" Width="69">
            	<Run Text="套接字连接信息:"/>
            </TextBlock>
            <Button Content="<-" HorizontalAlignment="Left" Margin="370,617,0,0" VerticalAlignment="Top" Width="86" Click="Button_Click_1"/>
        </Grid>

[C#]

        Socket socket;
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {

            //移动运营商
            textblock1.Text = DeviceNetworkInformation.CellularMobileOperator;



            //网络信息
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("网络是否可用:  ");
            sb.AppendLine(DeviceNetworkInformation.IsNetworkAvailable.ToString());
            sb.Append("是否启用蜂窝数据:  ");
            sb.AppendLine(DeviceNetworkInformation.IsCellularDataEnabled.ToString());
            sb.Append("是否允许漫游:  ");
            sb.AppendLine(DeviceNetworkInformation.IsCellularDataRoamingEnabled.ToString());
            sb.Append("是否启用Wi-Fi:  ");
            sb.AppendLine(DeviceNetworkInformation.IsWiFiEnabled.ToString());

            //网络接口信息
            sb.Append("网络接口类型:  ");
            var networkInterfaceType = NetworkInterface.NetworkInterfaceType;
            var networkInterfaceTypeString = networkInterfaceType.ToString() + "(" + InterfaceTypeConvert.Convert(networkInterfaceType) + ")";
            sb.AppendLine(networkInterfaceTypeString);

            sb.Append("网络接口子类型:  ");
            var networkSubInterfaceTypeString = string.Empty;
            DeviceNetworkInformation.ResolveHostNameAsync(new DnsEndPoint("www.baidu.com", 80),
                new NameResolutionCallback(x =>
                    {
                        networkSubInterfaceTypeString = x.NetworkInterface.InterfaceSubtype.ToString() + "[" + InterfaceTypeConvert.ConvertSub(x.NetworkInterface.InterfaceSubtype) + "]";

                        sb.AppendLine(networkSubInterfaceTypeString);
                        Dispatcher.BeginInvoke(() => { textblock2.Text = sb.ToString(); });
                    }), null);



            //套接字连接信息
            DnsEndPoint hostEntry = new DnsEndPoint("www.baidu.com", 80);
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //设置连接要求,必须为使用蜂窝技术网络
            socket.SetNetworkRequirement(NetworkSelectionCharacteristics.Cellular);
            SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
            socketEventArg.RemoteEndPoint = hostEntry;
            socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e1)
            {
                NetworkInterfaceInfo netInterfaceInfo = socket.GetCurrentNetworkInterface();
                StringBuilder sb2 = new StringBuilder();
                sb2.AppendLine("最后更新: " + DateTime.Now.ToString());
                sb2.Append("网络接口名: ");
                sb2.AppendLine(netInterfaceInfo.InterfaceName);
                sb2.Append("网络接口状态: ");
                sb2.AppendLine(netInterfaceInfo.InterfaceState.ToString());
                sb2.Append("网络接口类型: ");
                sb2.AppendLine(netInterfaceInfo.InterfaceType.ToString());
                sb2.Append("网络接口子类型: ");
                sb2.AppendLine(netInterfaceInfo.InterfaceSubtype.ToString());
                Dispatcher.BeginInvoke(() => textblock3.Text = sb2.ToString());
            });
            socket.ConnectAsync(socketEventArg);



            base.OnNavigatedTo(e);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));
        }
3. 检测网络状态改变

手机的网络连接并不如电脑稳定,当你到信号不好的地方,或者自动连接上WiFi等情况,怎么通过代码自动监测呢,下面演示了当手机网络改变时,列举出日志清单,并实时刷新网络信息。

[XAML]

    <!--LayoutRoot 是包含所有页面内容的根网格-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="网络状态改变测试" Style="{StaticResource PhoneTextNormalStyle}"/>
        </StackPanel>

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <TextBlock Grid.Row="0" Text="可用的网络接口" FontSize="{StaticResource PhoneFontSizeLarge}"/>
            <ListBox Grid.Row="1" x:Name="lbNetworkInterfaces" ItemsSource="{Binding}" Background="LightGray" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding}" Margin="5,5,0,5" Foreground="Black"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            <TextBlock Grid.Row="2" Text="事件日志" FontSize="{StaticResource PhoneFontSizeLarge}"/>
            <ScrollViewer Grid.Row="3" Background="LightGray" BorderThickness="1">
                <ListBox x:Name="lbNetworkChanges" ItemsSource="{Binding}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding}" Foreground="Black" 
                                    FontSize="{StaticResource PhoneFontSizeNormal}" TextWrapping="Wrap" Margin="5,10,0,10"  />
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </ScrollViewer>
            <TextBlock Grid.Row="4" Text="网络状态" FontSize="{StaticResource PhoneFontSizeLarge}"/>
            <Grid Grid.Row="5" Background="LightGray" >
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Row="0" Grid.Column="0" Text="网络是否可用" Foreground="Black" Margin="5,5,0,5" />
                <TextBlock Grid.Row="0" Grid.Column="1" x:Name="tbIsNetworkAvailable" HorizontalAlignment="Center" Foreground="Black" />

                <TextBlock Grid.Row="1" Grid.Column="0" Foreground="Black" Margin="5,5,0,5" >
                	<Run Text="WiFi是否可用"/>
                </TextBlock>
                <TextBlock Grid.Row="1" Grid.Column="1" x:Name="tbIsWiFiEnabled" HorizontalAlignment="Center" Foreground="Black"/>

                <TextBlock Grid.Row="2" Grid.Column="0" Text="是否启用蜂窝数据" Foreground="Black" Margin="5,5,0,5" />
                <TextBlock Grid.Row="2" Grid.Column="1" x:Name="tbIsCellularDataEnabled" HorizontalAlignment="Center" Foreground="Black"/>
            </Grid>
            <Button Grid.Row="6" x:Name="btnChangeNetworkSettings" Content="飞行" Click="Button_Click_3" Margin="314,0,46,0"/>
            <Button Content="->" Margin="391,0,-15,0" Grid.Row="6" Click="Button_Click_1"/>
            <Button Content="刷新" Margin="-15,0,370,0" Grid.Row="6" Click="Button_Click_2"/>
            <Button Content="WiFi" Margin="66,0,288,0" Grid.Row="6" Click="Button_Click_3" />
            <Button Content="网络" Margin="150,0,204,0" Grid.Row="6" Click="Button_Click_3" />
            <Button Content="蓝牙" Margin="232,0,122,0" Grid.Row="6" Click="Button_Click_3" />
        </Grid>
    </Grid>

[C#]

    public partial class Page1 : PhoneApplicationPage
    {

        public ObservableCollection<string> Changes { get; set; }

        public ObservableCollection<string> NetworkInterfaces { get; set; }

        public Page1()
        {
            InitializeComponent();


            // 网络可用性改变事件日志
            Changes = new ObservableCollection<string>();
            lbNetworkChanges.DataContext = Changes;

            //当前可用的网络接口
            NetworkInterfaces = new ObservableCollection<string>();
            lbNetworkInterfaces.DataContext = NetworkInterfaces;
            //网络可用性改变时
            DeviceNetworkInformation.NetworkAvailabilityChanged += new EventHandler<NetworkNotificationEventArgs>(ChangeDetected);

            UpdateNetworkInterfaces();
            UpdateNetworkStatus();
        }

        void ChangeDetected(object sender, NetworkNotificationEventArgs e)
        {
            string change = string.Empty;
            switch (e.NotificationType)
            {
                case NetworkNotificationType.InterfaceConnected:
                    change = "连接: ";
                    break;
                case NetworkNotificationType.InterfaceDisconnected:
                    change = "断开: ";
                    break;
                case NetworkNotificationType.CharacteristicUpdate:
                    change = "变更: ";
                    break;
                default:
                    change = "未知: ";
                    break;
            }

            string changeInformation = String.Format(" {0} {1} {2} ({3})",
                        DateTime.Now.ToShortTimeString(), change, e.NetworkInterface.InterfaceName,
                        InterfaceTypeConvert.Convert(e.NetworkInterface.InterfaceType) + "," + InterfaceTypeConvert.ConvertSub(e.NetworkInterface.InterfaceSubtype));

            Dispatcher.BeginInvoke(() =>
            {
                Changes.Add(changeInformation);
                UpdateNetworkStatus();
                UpdateNetworkInterfaces();

            });

        }

        //更新网络接口
        private void UpdateNetworkInterfaces()
        {

            NetworkInterfaces.Clear();
            //获取网络接口列表
            NetworkInterfaceList networkInterfaceList = new NetworkInterfaceList();
            foreach (NetworkInterfaceInfo networkInterfaceInfo in networkInterfaceList)
            {
                NetworkInterfaces.Add(String.Format("{0} ({1},{2})",
                    networkInterfaceInfo.InterfaceName,
                    InterfaceTypeConvert.Convert(networkInterfaceInfo.InterfaceType),
                    InterfaceTypeConvert.ConvertSub(networkInterfaceInfo.InterfaceSubtype)));
            }
        }

        //更新网络状态
        private void UpdateNetworkStatus()
        {
            tbIsCellularDataEnabled.Text = (DeviceNetworkInformation.IsCellularDataEnabled) ? "是" : "否";
            tbIsNetworkAvailable.Text = (DeviceNetworkInformation.IsNetworkAvailable) ? "是" : "否";
            tbIsWiFiEnabled.Text = (DeviceNetworkInformation.IsWiFiEnabled) ? "是" : "否";
        }

        //连接设置
        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            var settringTypeString = string.Empty;
            var button = sender as Button;
            if (button != null) settringTypeString = Convert.ToString(button.Content);
            var settringType = ConnectionSettingsType.WiFi;
            switch (settringTypeString)
            {
                case "飞行": settringType = ConnectionSettingsType.AirplaneMode;
                    break;
                case "网络": settringType = ConnectionSettingsType.Cellular;
                    break;
                case "蓝牙": settringType = ConnectionSettingsType.Bluetooth;
                    break;
                default: break;
            }

            //连接设置选择框
            ConnectionSettingsTask connectionSettings = new ConnectionSettingsTask();

            // 连接设置的类型
            connectionSettings.ConnectionSettingsType = settringType;
            connectionSettings.Show();
        }

        //刷新
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            UpdateNetworkStatus();
            UpdateNetworkInterfaces();
            Changes.Clear();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

    }

五、访问web的几种方式

Windows phone中访问Internet的方式主要有:通过套接字、WebClient 类、HttpWebRequest 类、服务引用等。套接字我们已经讲过。我主要讲解后面三种。

WebClient 类:一般用于通过访问URL获取特定的资源,下面是使用方法:

[C#]

WebClient webClient = new WebClient();

webClient.DownloadStringAsync(new Uri("http://www.baidu.com", UriKind.Absolute));
webClient.DownloadStringCompleted += (a, b) =>
{
    if (b.Error == null && !b.Cancelled)
    {
        Dispatcher.BeginInvoke(() => MessageBox.Show(b.Result));
    }
};

HttpWebRequest 类:对http做了基本的实现,相当于比直接用socket方便一点。

[C#]

 Uri uri = new Uri("http://192.168.100.212");
 HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
 httpWebRequest.Method = "POST";
 //异步获取HTTP请求正文流
 httpWebRequest.BeginGetRequestStream(new AsyncCallback(a =>
 {
     var httpWebRequest2 = a.AsyncState as HttpWebRequest;
     var stream = httpWebRequest2.EndGetRequestStream(a);
     var streamWriter = new StreamWriter(stream);
     //在HTTP请求正文流中写入要POST的参数
     streamWriter.WriteLine("wd=111");
     streamWriter.Close();
     //开始异步的向远程主机发送HTTP请求
     httpWebRequest2.BeginGetResponse(new AsyncCallback(b =>
     {
         try
         {
             var httpWebRequest3 = b.AsyncState as HttpWebRequest;
             //请求执行完成,得到HTTP相应
             WebResponse webResponse = httpWebRequest3.EndGetResponse(b);
             var s = webResponse.ContentType;
             //获取到HTTP相应正文流
             var stream3 = webResponse.GetResponseStream();
             //读出来,转换成字符串
             var streamReader = new StreamReader(stream3, Encoding.UTF8);
             var text = streamReader.ReadToEnd();

             Dispatcher.BeginInvoke(() => MessageBox.Show(text));
         }
         catch (Exception ex)
         {
             var c = ex.Message;
         }

     }), httpWebRequest2);

 }), httpWebRequest);

服务引用:其实于一般的服务引用一样,只不过这里只支持异步实现。

[C#]

//添加服务引用后,下面代码为异步调用web服务
ServiceReference1.WebService1SoapClient c = new ServiceReference1.WebService1SoapClient();
c.HelloWorldAsync("");
c.HelloWorldCompleted += (a, b) =>
{
    Dispatcher.BeginInvoke(() => MessageBox.Show(b.Result));
};

Windows phone 8 学习笔记(3) 通信(转),布布扣,bubuko.com

Windows phone 8 学习笔记(3) 通信(转)

上一篇:我来出个题:这个事务会不会回滚?


下一篇:Spring Cloud课程干货总结第八期—微服务架构的设计策略