2

终于找到了为什么有些用户报告崩溃而其他用户说他们没有问题。我使用 NetworkService 类来检查互联网连接。但显然这不适用于 WP 8。有没有办法重做这个,所以它可以在两个操作系统上工作?我的 NetworkServices.cs 的这一行出现异常

返回 profile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;

网络服务.cs:

{
public class NetworkService : INetworkService
{
    public bool IsConnectionAvailable
    {
        get
        {
            ConnectionProfile profile = NetworkInformation.GetInternetConnectionProfile();

            if (profile == null)
            {
                return false;
            }

            return profile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
        }
    }
}

相关 MainPage.cs:

if (data.IsDownloaded)
        {
            this.PlaySound(IsolatedStorageFile.GetUserStoreForApplication().OpenFile(data.SavePath, FileMode.Open, FileAccess.Read, FileShare.Read));
        }
        else
        {
            if (!SimpleIoc.Default.GetInstance<INetworkService>().IsConnectionAvailable)
            {
                MessageBox.Show("You need an network or cellular connection to download.");
            }

堆栈跟踪:

{System.NotImplementedException:方法或操作未实现。在 Windows.Networking.Connectivity.ConnectionProfile.GetNetworkConnectivityLevel() 在 App.Services.NetworkService.get_IsConnectionAvailable() 在 App.MainPage.LongListSelector_SelectionChanged(Object sender, SelectionChangedEventArgs e) 在 Microsoft.Phone.Controls.LongListSelector.set_SelectedItem(Object value) 在Microsoft.Phone.Controls.LongListSelector.OnItemTap(Object sender, GestureEventArgs e) 在 MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args) 在 MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)} System.Exception {System.NotImplementedException}

4

1 回答 1

2

Windows Phone 8 上的NetworkInformation.GetInternetConnectionProfile()是一个较新的基于 WinRT 的 API,但不幸的是,大多数方法看起来都没有实现。

在运行时,使用调试器,您会看到几乎所有的属性都会抛出 type 的异常NotImplementedException。但是由于方法和属性被存根,Visual Studio 并没有更好地了解并允许编译代码。

我已经在 WP8 SL、WP8.1 SL、WP8.1 XAML 项目中使用 WP8 和 WP8.1 模拟器测试了以下方法,在可能的情况下,以确认以下...


Windows Phone 7.5 和 8.0

由于NetworkInformation.GetInternetConnectionProfile()两者都没有完全实现(或者根本没有在 7.5 上实现),MSDN 文档建议......

DeviceNetworkInformation.IsNetworkAvailable();

注意:这仍然可以在 Windows Phone 8.1 Silverlight 中使用


Windows Phone 8.1 SL 和 XAML/WinRT

由于 Windows 8 和 Windows Phone 8 之间的日益融合,几乎所有 WinRT API 现在都可在 Windows Phone 8.1 中使用。如果您正在制作 Windows Phone 8.1(SL 或 XAML)或 Universal all(Windows 8.1 + Windows Phone 8.1),那么使用最新的 API 是有意义的......

NetworkInformation.GetInternetConnectionProfile() 
于 2014-07-16T15:25:31.903 回答