Edit

Share via


NetworkHelper

It exposes network information though a property called ConnectionInformation. The ConnectionInformation holds information about ConnectionType, ConnectivityLevel, ConnectionCost, SignalStrength, Internet Connectivity and more.

What is a metered connection? A metered connection is an Internet connection that has a data limit or cost associated with it. Cellular data connections are set as metered by default. Wi-Fi network connections can be set to metered, but aren't by default. Application developers should take metered nature of connection into account and reduce data usage.

<!--  Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information.  -->
<Page x:Class="HelpersExperiment.Samples.NetworkHelperSample"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:local="using:HelpersExperiment.Samples"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d">

    <StackPanel Orientation="Vertical">
        <TextBlock>
            <Run FontWeight="SemiBold"
                 Text="Internet available: " />
            <Run x:Name="IsInternetAvailableText" />
        </TextBlock>

        <TextBlock>
            <Run FontWeight="SemiBold"
                 Text="On metered connection: " />
            <Run x:Name="IsInternetOnMeteredConnectionText" />
        </TextBlock>

        <TextBlock>
            <Run FontWeight="SemiBold"
                 Text="Connection type: " />
            <Run x:Name="ConnectionTypeText" />
        </TextBlock>

        <TextBlock>
            <Run FontWeight="SemiBold"
                 Text="Signal bars: " />
            <Run x:Name="SignalBarsText" />
        </TextBlock>

        <TextBlock>
            <Run FontWeight="SemiBold"
                 Text="Network names: " />
            <Run x:Name="NetworkNamesText" />
        </TextBlock>
    </StackPanel>
</Page>
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CommunityToolkit.WinUI.Helpers;

namespace HelpersExperiment.Samples;

[ToolkitSample(id: nameof(NetworkHelperSample), "Network Helper", description: $"A sample for showing how to use {nameof(NetworkHelper)}.")]
public sealed partial class NetworkHelperSample : Page
{
    public NetworkHelperSample()
    {
        this.InitializeComponent();
        Load();
    }

    private void Load()
    {
        IsInternetAvailableText.Text = NetworkHelper.Instance.ConnectionInformation.IsInternetAvailable ? "Yes" : "No";
        IsInternetOnMeteredConnectionText.Text = NetworkHelper.Instance.ConnectionInformation.IsInternetOnMeteredConnection ? "Yes" : "No";
        ConnectionTypeText.Text = NetworkHelper.Instance.ConnectionInformation.ConnectionType.ToString();
        SignalBarsText.Text = NetworkHelper.Instance.ConnectionInformation.SignalStrength.GetValueOrDefault(0).ToString();
        NetworkNamesText.Text = string.Join(", ", NetworkHelper.Instance.ConnectionInformation.NetworkNames);
    }
}