[英]Referencing WinRT/UWP libraries in a .NET desktop application while maintaining support for Windows 7
我正在嘗試在桌面應用程序中引用“ Windows.Networking.Connectivity”類。 我基本上對處理應用程序中的計量連接感興趣。
基本上我想做的很簡單:
var connectionCost = NetworkInformation.GetInternetConnectionProfile().GetConnectionCost();
if (connectionCost.NetworkCostType == NetworkCostType.Unknown
|| connectionCost.NetworkCostType == NetworkCostType.Unrestricted)
{
//Connection cost is unknown/unrestricted
}
else
{
//Metered Network
}
我知道的唯一允許桌面應用程序引用UWP程序集的方法是手動編輯項目文件,並將以下行添加到csproj文件中:
<TargetPlatformVersion>8.0</TargetPlatformVersion>
應用代碼和“ hack”工作正常,但問題是這樣做會阻止我的應用程序在我需要支持的Windows 7上運行。
我想知道是否有一種方法可以引用桌面應用程序中的UWP程序集而不必放棄對Windows 7的支持。
而且由於暫時我只想檢查連接是否已計量,因此我願意接受有關如何在不引用Windows程序集的情況下獲取此信息的建議。
我找到了一種無需指定目標平台即可使用反射和調用UWP方法的方法。 就我而言,這就是我所做的:
var networkInfoType = Type.GetType("Windows.Networking.Connectivity.NetworkInformation, Windows, ContentType=WindowsRuntime");
var profileType = Type.GetType("Windows.Networking.Connectivity.NetworkInformation, Windows, ContentType=WindowsRuntime");
var profileObj = networkInfoType.GetTypeInfo().GetDeclaredMethod("GetInternetConnectionProfile").Invoke(null, null);
dynamic profDyn = profileObj;
var costObj = profDyn.GetConnectionCost();
dynamic dynCost = costObj;
var costType = (NetworkCostType)dynCost.NetworkCostType;
if (costType == NetworkCostType.Unknown
|| costType == NetworkCostType.Unrestricted)
{
//Connection cost is unknown/unrestricted
}
else
{
//Metered Network
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.