[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 ] 订阅

目录
相关文章
|
安全 数据安全/隐私保护 Windows
如何在Windows 10系统中查看已连接WiFi密码-亲测可用-优雅草卓伊凡
如何在Windows 10系统中查看已连接WiFi密码-亲测可用-优雅草卓伊凡
1322 16
如何在Windows 10系统中查看已连接WiFi密码-亲测可用-优雅草卓伊凡
|
8月前
|
Unix Shell Windows
Windows PowerShell技巧:使用findstr实现类似grep的功能
显示带有线路编号**: `/N`选项将显示每条结果前面带有其在线路上出现位置编号。
1348 7
|
传感器 数据采集 移动开发
基于STM32的智能手环wifi连接手机APP(下)
基于STM32的智能手环wifi连接手机APP(下)
1272 0
|
人工智能 数据处理 C#
AI Dev Gallery:微软开源 Windows AI 模型本地运行工具包和示例库,助理开发者快速集成 AI 功能
微软推出的AI Dev Gallery,为Windows开发者提供开源AI工具包和示例库,支持本地运行AI模型,提升开发效率。
1095 13
|
安全 BI 数据安全/隐私保护
AD域、Windows AD域管理功能大全
Windows AD域管理功能大全,再也不用东拼西凑了!
1298 1
|
人工智能 JavaScript 网络安全
ToB项目身份认证AD集成(三完):利用ldap.js实现与windows AD对接实现用户搜索、认证、密码修改等功能 - 以及针对中文转义问题的补丁方法
本文详细介绍了如何使用 `ldapjs` 库在 Node.js 中实现与 Windows AD 的交互,包括用户搜索、身份验证、密码修改和重置等功能。通过创建 `LdapService` 类,提供了与 AD 服务器通信的完整解决方案,同时解决了中文字段在 LDAP 操作中被转义的问题。
739 1
|
Linux Android开发 iOS开发
Windows平台RTSP|RTMP播放器如何实现实时录像功能
Windows平台RTSP、RTMP播放器实时录像接口设计,实际上,除了Windows平台,我们Linux、Android、iOS平台也是一样的设计,单纯的录像模块,如果做的全面,也不是一两个接口可以搞定的
540 1
|
传感器 存储 编解码
基于STM32的智能手环wifi连接手机APP(上)
基于STM32的智能手环wifi连接手机APP(上)
839 0
|
存储 开发者 C#
WPF与邮件发送:教你如何在Windows Presentation Foundation应用中无缝集成电子邮件功能——从界面设计到代码实现,全面解析邮件发送的每一个细节密武器!
【8月更文挑战第31天】本文探讨了如何在Windows Presentation Foundation(WPF)应用中集成电子邮件发送功能,详细介绍了从创建WPF项目到设计用户界面的全过程,并通过具体示例代码展示了如何使用`System.Net.Mail`命名空间中的`SmtpClient`和`MailMessage`类来实现邮件发送逻辑。文章还强调了安全性和错误处理的重要性,提供了实用的异常捕获代码片段,旨在帮助WPF开发者更好地掌握邮件发送技术,提升应用程序的功能性与用户体验。
464 0