[Windows Phone] 如何撰写连接 Wifi、蓝芽、网路、飞航模式的网路设定功能

简介: 原文:[Windows Phone] 如何撰写连接 Wifi、蓝芽、网路、飞航模式的网路设定功能 前言 为了可以使自己的 APP 具备操作网路的功能,在本文分享研究心得,包含在 Windows Phone 应用程式做到连接 Wifi、连接蓝芽、连接网路、飞航模式设定等网路功能和程式码。
原文: [Windows Phone] 如何撰写连接 Wifi、蓝芽、网路、飞航模式的网路设定功能

前言

为了可以使自己的 APP 具备操作网路的功能,在本文分享研究心得,包含在 Windows Phone 应用程式做到连接 Wifi、连接蓝芽、连接网路、飞航模式设定等网路功能和程式码。

?

撰写方法

步骤一、建立一个专案。

?

步骤二、 画面设计

在手机页面放四个 Button,方别做连接 Wifi、连接蓝芽、连接网路和飞航模式设定:

  1. Button 控制项,Name 属性:btnWifi、Content 属性:连接 Wifi
  2. Button 控制项,Name 属性:btnBT? 、Content 属性:连接蓝芽
  3. Button 控制项,Name 属性:btnWeb、Content 属性:连接网路
  4. Button 控制项,Name 属性:btnAir? 、Content 属性:飞航模式设定

如图所示:

?

产生的XAML程式码如下:

        <!--ContentPanel - 其他内容置於此-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Button x:Name="btnWifi" Content="连接 Wifi" HorizontalAlignment="Left" Margin="75,60,0,0" VerticalAlignment="Top" Width="323"/>
            <Button x:Name="btnBT" Content="连接蓝芽" HorizontalAlignment="Left" Margin="75,149,0,0" VerticalAlignment="Top" Width="323"/>
            <Button x:Name="btnWeb" Content="连接网路" HorizontalAlignment="Left" Margin="75,226,0,0" VerticalAlignment="Top" Width="323"/>
            <Button x:Name="btnAir" Content="飞航模式设定" HorizontalAlignment="Left" Margin="75,317,0,0" VerticalAlignment="Top" Width="323"/>
        </Grid>

?

步骤三、在 MainPage.cs 程式码中撰写事件处理函式:

?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using NetworkApp.Resources;

// 加入 Microsoft.Phone.Tasks
using Microsoft.Phone.Tasks;

namespace NetworkApp
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 建构函式
        public MainPage()
        {
            InitializeComponent();

            btnWifi.Click += btnWifi_Click;
            btnBT.Click += btnBT_Click;
            btnWeb.Click += btnWeb_Click;
            btnAir.Click += btnAir_Click;
        }

        // 指定连接设定 Wifi
        void btnWifi_Click(object sender, RoutedEventArgs e)
        {
            ConnectionSettingsTask cn = new ConnectionSettingsTask();
            cn.ConnectionSettingsType = ConnectionSettingsType.WiFi;
            cn.Show();
        }

        // 指定连接设定蓝芽
        void btnBT_Click(object sender, RoutedEventArgs e)
        {
            ConnectionSettingsTask cn = new ConnectionSettingsTask();
            cn.ConnectionSettingsType = ConnectionSettingsType.Bluetooth;
            cn.Show();
        }

        // 指定连接手机网路
        void btnWeb_Click(object sender, RoutedEventArgs e)
        {
            ConnectionSettingsTask cn = new ConnectionSettingsTask();
            cn.ConnectionSettingsType = ConnectionSettingsType.Cellular;
            cn.Show();
        }

        // 设定飞航模式
        void btnAir_Click(object sender, RoutedEventArgs e)
        {
            ConnectionSettingsTask cn = new ConnectionSettingsTask();
            cn.ConnectionSettingsType = ConnectionSettingsType.AirplaneMode;
            cn.Show();
        }
    }
}

?

结果

程式一开始执行如下图所示:

?

连接 Wifi。

?

连接蓝芽。

?

连接网路。

?

飞航模式设定。

?

结语

撰写网路设定的方法很简单,短短的几段程式码就可以使自己的 APP 又多了一个功能,希望可以给大家一个参考。

?

相关参考与引用

Microsoft.Phone.Tasks Namespace

?

范例下载

NetworkApp.zip


posted on 2013/11/16 22:31 | 阅读数 : 615 2 人推荐 我要推荐 | Add Comment | 文章分类 [ Windows Phone ] 订阅

目录
相关文章
|
10月前
|
安全 数据安全/隐私保护 Windows
如何在Windows 10系统中查看已连接WiFi密码-亲测可用-优雅草卓伊凡
如何在Windows 10系统中查看已连接WiFi密码-亲测可用-优雅草卓伊凡
861 16
如何在Windows 10系统中查看已连接WiFi密码-亲测可用-优雅草卓伊凡
|
传感器 数据采集 移动开发
基于STM32的智能手环wifi连接手机APP(下)
基于STM32的智能手环wifi连接手机APP(下)
1020 0
|
Ubuntu 网络安全 数据安全/隐私保护
ubuntu server连接wifi教程
本文提供了一个简化Ubuntu Server在Raspberry Pi系统上配置过程的脚本"config_ubuntu_server",包括自动和手动两种方法来设置root权限、SSH配置,并连接WiFi,同时支持无密码SSH访问,适合初学者和高级用户。
741 3
|
传感器 存储 编解码
基于STM32的智能手环wifi连接手机APP(上)
基于STM32的智能手环wifi连接手机APP(上)
686 0
|
Android开发
Android获取当前连接的wifi名称
Android获取当前连接的wifi名称
874 6
|
网络协议
了解AT指令以及STM32F103如何通过ESP8266连接到WiFi
AT指令是一组用于控制调制解调器的命令,最早由Hayes公司为其智能调制解调器开发。如今,AT指令已被广泛应用于各种通信模块中,包括GSM、Bluetooth和WiFi模块。AT指令通常以“AT”开头,后跟特定的命令和参数。通过这些指令,我们可以执行一系列操作,如设置网络参数、发送数据和查询状态等。
1127 0
|
数据安全/隐私保护 Windows
windows系统bat批处理 笔记本开wifi 笔记本查看wifi密码
windows系统bat批处理 笔记本开wifi 笔记本查看wifi密码
542 0
|
数据安全/隐私保护 Windows
windows系统bat批处理 查看当前电脑连接过的wifi名字和wifi密码
windows系统bat批处理 查看当前电脑连接过的wifi名字和wifi密码
683 0
|
Android开发
android连接指定wifi
android连接指定wifi
638 0
|
Java Android开发
Android 9在连接以太网情况下 还能连接WiFi
Android 9在连接以太网情况下 还能连接WiFi
242 0