0

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.

I am using the following namespaces...

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

Tried using Bool, IPHostEntry, GetIsNetworkAvailable but everthing failed.

Can someone post a sample code? Will it be in app.xaml.cs and individual page's cs?

Below image is my try.enter image description here

Ganesh . G
  • 25
  • 4

3 Answers3

1

I found some help here Check Internet Connection and Navigate to Pages and managed to do what I wanted.

I write my code in MainPage.xaml.cs under "protected override void 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. Below is the code.

enter image description here

Community
  • 1
  • 1
Ganesh . G
  • 25
  • 4
0

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.

That said, to answer the actual question, I think it makes sense to look into NetworkAvailabilityChanged event.

Tutorial at MSDN seems to be pretty comprehensive.

Erki M.
  • 5,022
  • 1
  • 48
  • 74
0

as you want to check the internet availability when your app is launched, you should move your code logic to inside method 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.

I Putu Yoga Permana
  • 3,980
  • 29
  • 33