介绍
重新想象 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 - 用于对网络信息的访问 */ // 获取当前用于 Internet 连接的 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; } } }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索网络
, text
, using
, environment
, public
cp通信
,以便于您获取更多的相关知识。
时间: 2024-11-03 10:59:24