Windows 8 Store Apps學習(60) 通訊: 擷取網路資訊, 序列化和還原序列化

來源:互聯網
上載者:User

介紹

重新想象 Windows 8 Store Apps 之 通訊

擷取網路資訊

序列化 - json

序列化 - xml

序列化 - rss atom

樣本

1、示範如何擷取網路的相關資訊

Communication/NetworkInfo.xaml.cs

/* * 示範如何擷取網路的相關資訊 */    using System;using System.Collections.Generic;using Windows.Networking.Connectivity;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Navigation;using System.Linq;    namespace XamlDemo.Communication{    public sealed partial class NetworkInfo : Page    {        public NetworkInfo()        {            this.InitializeComponent();        }            protected override void OnNavigatedTo(NavigationEventArgs e)        {            /*             * NetworkInformation - 用於對網路資訊的訪問             */                // 擷取當前用於 網際網路連線的 ConnectionProfile 對象            ConnectionProfile connectionProfile = NetworkInformation.GetInternetConnectionProfile();                if (connectionProfile == null)                return;                // 此串連配置的名稱            lblMsg.Text = "ProfileName: " + connectionProfile.ProfileName;            lblMsg.Text += Environment.NewLine;                // 此串連的網路連接層級(Windows.Networking.Connectivity.NetworkConnectivityLevel 枚舉)            //     None - 無串連            //     LocalAccess - 僅允許訪問本網            //     ConstrainedInternetAccess - 受限的 internet 訪問            //     InternetAccess - 本地和 internet 訪問            lblMsg.Text += "NetworkConnectivityLevel: " + connectionProfile.GetNetworkConnectivityLevel();            lblMsg.Text += Environment.NewLine;                // 網路狀態發生變化時所觸發的事件            NetworkInformation.NetworkStatusChanged += NetworkInformation_NetworkStatusChanged;                NetworkAdapter networkAdapter = connectionProfile.NetworkAdapter;            if (networkAdapter != null)            {                lblMsg.Text += "NetworkAdapterId: " + networkAdapter.NetworkAdapterId; // 網路介面卡 ID                lblMsg.Text += Environment.NewLine;                lblMsg.Text += "InboundMaxBitsPerSecond: " + networkAdapter.InboundMaxBitsPerSecond; // 最大入站資料轉送速率(單位:bit/s)                lblMsg.Text += Environment.NewLine;                lblMsg.Text += "OutboundMaxBitsPerSecond: " + networkAdapter.OutboundMaxBitsPerSecond; // 最大出站資料轉送速率(單位:bit/s)                lblMsg.Text += Environment.NewLine;                lblMsg.Text += "NetworkTypes: " + networkAdapter.NetworkItem.GetNetworkTypes(); // 網路類型                lblMsg.Text += Environment.NewLine;            }                lblMsg.Text += Environment.NewLine;                // 擷取所有可用串連            IReadOnlyList<ConnectionProfile> connectionProfiles = NetworkInformation.GetConnectionProfiles();            foreach (ConnectionProfile cp in connectionProfiles)            {                lblMsg.Text += "ProfileName: " + cp.ProfileName;                lblMsg.Text += Environment.NewLine;                    // 擷取此串連的指定時間段內的本機資料的使用方式                DataUsage dataUsage = cp.GetLocalUsage(DateTime.Now.AddHours(-1), DateTime.Now);                    lblMsg.Text += "BytesSent: " + dataUsage.BytesSent; // 已發送的位元組數                lblMsg.Text += Environment.NewLine;                lblMsg.Text += "BytesReceived: " + dataUsage.BytesReceived; // 已收到的位元組數                lblMsg.Text += Environment.NewLine;            }                    // 以下是一些不常用的東西            ConnectionCost connectionCost = connectionProfile.GetConnectionCost();            DataPlanStatus dataPlanStatus = connectionProfile.GetDataPlanStatus();            NetworkSecuritySettings networkSecuritySettings = connectionProfile.NetworkSecuritySettings;            IReadOnlyList<LanIdentifier> lanIdentifiers = NetworkInformation.GetLanIdentifiers();        }            void NetworkInformation_NetworkStatusChanged(object sender)        {            }    }}

用於示範序列化和還原序列化的實體類

Communication/Serializer/Product.cs

/* * 用於示範序列化和還原序列化的實體類 *  * 通過 DataContractJsonSerializer 或 DataContractSerializer 做序列化和還原序列化時,其支援 DataContract, DataMember, KnownType * 當然如果都不聲明 DataContract, DataMember 也沒問題 */    using System;using System.Collections.Generic;using System.Runtime.Serialization;    namespace XamlDemo.Communication.Serializer{    [DataContract(Name="product")]    public class Product    {        [DataMember(Name = "productId", IsRequired = true, Order = 1)]        public int ProductId { get; set; }            [DataMember(Name = "name", IsRequired = true, Order = 2)]        public string Name { get; set; }            public decimal Price { get; set; }            [DataMember(Name = "createTime", IsRequired = true, Order = 3)]        public DateTime CreateTime { get; set; }            public static List<Product> GetProducts()        {            List<Product> products = new List<Product>();            for (int i = 0; i < 5; i++)            {                products.Add(new Product                {                    ProductId = i,                    Name = "name: " + i.ToString().PadLeft(4, '0'),                    Price = i * 100,                    CreateTime = DateTime.Now.AddDays(-i)                });            }                return products;        }    }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.