简体   繁体   English

Internet可用性检查-导航到页面-Windows Phone 8.1-C#

[英]Internet availability check - Navigate to Page - Windows Phone 8.1 - C#

I am a newbie! 我是新手! I want to check the internet availability when my app is launched as i am retrieving data from server. 当我从服务器检索数据时,我想检查启动我的应用程序时的互联网可用性。 If internet is available, User can carry-on with app without any notification. 如果可以使用互联网,则用户可以随身携带应用程序,而无需任何通知。 If there in no internet, then I have to navigate to a specific page from my app. 如果没有互联网,那么我必须从我的应用程序导航到特定页面。

I found some pages on stack overflow for this but there are old and for windows 8 or windows phone 8 not for WP8.1. 我为此找到了一些堆栈溢出的页面,但其中有些页面很旧,并且对于Windows 8或Windows Phone 8而言不是WP8.1。

I am using the following namespaces... 我正在使用以下名称空间...

System.Net.NetworkInformation, Windows.System, Windows.Networking.Connectivity and System.Net; System.Net.NetworkInformation,Windows.System,Windows.Networking.Connectivity和System.Net;

Tried using Bool, IPHostEntry, GetIsNetworkAvailable but everthing failed. 使用Bool,IPHostEntry,GetIsNetworkAvailable进行了尝试,但是一切都失败了。

Can someone post a sample code? 有人可以发布示例代码吗? Will it be in app.xaml.cs and individual page's cs? 它会出现在app.xaml.cs和单个页面的CS中吗?

Below image is my try. 下图是我的尝试。 在此处输入图片说明

I found some help here Check Internet Connection and Navigate to Pages and managed to do what I wanted. 我在这里找到了一些帮助。 检查Internet连接导航到页面,并设法完成了我想做的事情。

I write my code in MainPage.xaml.cs under "protected override void OnNavigatedTo(NavigationEventArgs e)". 我在MainPage.xaml.cs中的“保护的重写无效的OnNavigatedTo(NavigationEventArgs e)”下编写我的代码。 I am planning to execute this code on every page instead of registering for network change notification. 我打算在每个页面上执行此代码,而不是注册网络更改通知。 Since this code uses await function, I added Async to the OnNavigatedTo event. 由于此代码使用了await函数,因此我将Async添加到OnNavigatedTo事件。 Below is the code. 下面是代码。

在此处输入图片说明

Firstly, looking at the picture you provided, it seems you are trying to write code to class level. 首先,看一下您提供的图片,看来您正在尝试将代码编写到类级别。 As far as I know, you can not really write if-else logic etc to class level, this should be a method-level code. 据我所知,您不能真正将if-else逻辑等编写到类级别,这应该是方法级别的代码。

That said, to answer the actual question, I think it makes sense to look into NetworkAvailabilityChanged event . 就是说,为了回答实际问题,我认为调查NetworkAvailabilityChanged事件是有意义的。

Tutorial at MSDN seems to be pretty comprehensive. MSDN上的教程似乎非常全面。

as you want to check the internet availability when your app is launched , you should move your code logic to inside method OnLaunched: 当您要在启动应用程序时检查Internet可用性时,应将代码逻辑移至内部方法OnLaunched中:

protected override void OnLaunched(LaunchActivatedEventArgs e)
{
   //paste your code here to check network
}

you can do it by click plus button on the left side method, to open bracket, and paste your code inside it. 您可以通过单击左侧方法上的加号按钮来打开括号,然后将代码粘贴到其中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM