Since Silverlight 3, it’s possible to detect changes in the state of network availability. This can be useful to check whether or not a service call can be executed. In combination with the possibility to build a Silverlight application that runs out-of-browser, this feature opens up ways to build occasionally connected system. In this article, we’ll look how we build an application that detects changes in the network state. The code for this article can be downloaded here.
Check network stateFor this article, I’ve put together a very basic interface that can be seen below. The application knows whether it’s online or offline. Also, it knows whether or not it’s running inside the browser or not.
The XAML for this interface is shown below.<Border BorderBrush="Red" Name="MainBorder"
BorderThickness="4" Width="400" Height="200"> <StackPanel Orientation="Vertical">
<TextBlock FontSize="32" Name="ConnectionStatusTextBlock"
HorizontalAlignment="Center" VerticalAlignment="Center">
</TextBlock>
<TextBlock FontSize="32" Name="ApplicationStatusTextBlock"
HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</StackPanel></Border>Checking changes in the network can be done using the NetworkChange class. This is a very simple class: it defines just one event called NetworkAddressChanged. This event is triggered by Silverlight automatically when it notices that the IP address has changed, meaning that either a network card was plugged-in or unplugged. To react to changes, we need to subscribe to this event. In the sample, we are doing so using the following code: NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged); void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
{
CheckNetworkStatus();
}
private void CheckNetworkStatus()
{
if (NetworkInterface.GetIsNetworkAvailable())
{
MainBorder.BorderBrush = new SolidColorBrush(Colors.Green);
ConnectionStatusTextBlock.Text = "Connected";
}
else
Read more: SilverlightShow
QR:
Check network stateFor this article, I’ve put together a very basic interface that can be seen below. The application knows whether it’s online or offline. Also, it knows whether or not it’s running inside the browser or not.
BorderThickness="4" Width="400" Height="200"> <StackPanel Orientation="Vertical">
<TextBlock FontSize="32" Name="ConnectionStatusTextBlock"
HorizontalAlignment="Center" VerticalAlignment="Center">
</TextBlock>
<TextBlock FontSize="32" Name="ApplicationStatusTextBlock"
HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</StackPanel></Border>Checking changes in the network can be done using the NetworkChange class. This is a very simple class: it defines just one event called NetworkAddressChanged. This event is triggered by Silverlight automatically when it notices that the IP address has changed, meaning that either a network card was plugged-in or unplugged. To react to changes, we need to subscribe to this event. In the sample, we are doing so using the following code: NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged); void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
{
CheckNetworkStatus();
}
private void CheckNetworkStatus()
{
if (NetworkInterface.GetIsNetworkAvailable())
{
MainBorder.BorderBrush = new SolidColorBrush(Colors.Green);
ConnectionStatusTextBlock.Text = "Connected";
}
else
Read more: SilverlightShow
QR: