2015-09-29 47 views

回答

21

在UWP中,您可以使用IsWlanConnectionProfile或IsWwanConnectionProfile屬性檢查網絡連接。

一個例子是:

var temp = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile(); 

if (temp.IsWlanConnectionProfile) 
{ 
    // its wireless 
}else if (temp.IsWwanConnectionProfile) 
{ 
    // its mobile 
} 

我希望這有助於。

+2

你應該檢查'temp'是否爲null。 – Nick

9

除了獲得連通性(其他人已經提到)之外,您還可以更好地處理計量連接。

How to manage metered network cost constraints

switch (connectionCost.NetworkCostType) 
{ 
    case NetworkCostType.Unrestricted: 
     // 
     break; 
    case NetworkCostType.Fixed: 
     // 
     break; 
    case NetworkCostType.Variable: 
     // 
     break; 
    case NetworkCostType.Unknown: 
     // 
     break; 
    default: 
     // 
     break; 
} 

networking demo at GitHub

if (connectionCost.Roaming || connectionCost.OverDataLimit) 
{ 
    Cost = NetworkCost.OptIn; 
    Reason = connectionCost.Roaming 
     ? "Connection is roaming; using the connection may result in additional charge." 
     : "Connection has exceeded the usage cap limit."; 
} 
else if (connectionCost.NetworkCostType == NetworkCostType.Fixed 
    || connectionCost.NetworkCostType == NetworkCostType.Variable) 
{ 
    Cost = NetworkCost.Conservative; 
    Reason = connectionCost.NetworkCostType == NetworkCostType.Fixed 
     ? "Connection has limited allowed usage." 
     : "Connection is charged based on usage. "; 
} 
else 
{ 
    Cost = NetworkCost.Normal; 
    Reason = connectionCost.NetworkCostType == NetworkCostType.Unknown 
     ? "Connection is unknown" 
     : "Connection cost is unrestricted"; 
} 
相關問題