介紹
重新想象 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; } }}