[英]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.