kikis
kikis

Reputation: 103

Xamarin connectivity change transform function

I am learning Xamarin. I would like to change this function:

 public void CheckWifiContinuously()
 {
        CrossConnectivity.Current.ConnectivityChanged += (sender, args) =>
        {
            Conn = args.IsConnected ? true : false;
        };
 }

into a function that returns args.IsConnected (a boolean)

I take the code from here.

Thanks for your help.

Upvotes: 0

Views: 65

Answers (2)

GeralexGR
GeralexGR

Reputation: 3592

You could use Xamarin.Essentials and specifically Connectivity class as CrossConnectivity features have been consolidated into Xamarin.Essentials toolkit.

private bool CheckInternet()
        {
            var current = Connectivity.NetworkAccess;

            if (current == NetworkAccess.Internet) return true;
            else return false;
        }

Upvotes: 1

MilanG
MilanG

Reputation: 2604

The code which you have used is to track the network state changed event. What you want to achieve is "How to check if Network is available or not". You can do this by following:

if(CrossConnectivity.Current.IsConnected)
{
    //Connected
}
else
{
  //Not Connected
}

You actually not need to create a separate function. You can use this property directly as I mentioned above. Hope this helps!

Upvotes: 1

Related Questions