Reputation: 71
I am developing a WP7 app, which needs online connectivity. Before navigating to any page from mainpage.xaml, I am checking whether or not the device is connected to the internet using:
isAvailable = NetworkInterface.GetIsNetworkAvailable();
The app only continues if isAvailable
is true; otherwise, it shows a pop stating "Please check your WI-FI connectivity"
Question: Initially, the app is connected to the internet, but how should I handle situations where internet connectivity is lost while I'm using the app, but after the initial check?
Should I check for isAvailable = NetworkInterface.GetIsNetworkAvailable();
on every page load or before every service call? Is there a better way of implementing this?
Upvotes: 1
Views: 1199
Reputation: 647
using Microsoft.Phone.Net.NetworkInformation;
public MainPage()
{
InitializeComponent();
if (NetworkInterface.GetIsNetworkAvailable())
{
// Use navigation method here
}
else
MessageBox.Show("Need net connection");
Upvotes: -2
Reputation: 3294
You should hook into the DeviceNetworkInformation.NetworkAvailabilityChanged Event which will allow your application to be notified when the network availability changes, removing the need to check every time you perform a service call :-
Upvotes: 5
Reputation: 39007
No point on checking NetworkInterface.GetIsNetworkAvailable on every page. A web request can fail even is the user has internet connectivity, and in fact it will happen pretty often. Just make sure to handle all the error cases, provide automatic or easy-to-use retry mechanisms, and notify the user that the action cannot complete because something is wrong with its connection.
Upvotes: 2