Bernhard's profileBernhard Grojer - BlogPhotosBlogLists Tools Help

Blog


    April 07

    Silverlight 3: NetworkChange (u. DataBinding mit INotifyPropertyChanged)

    In Silverlight 3 gibt es nun eine einfache Möglichkeit um den aktuellen Netzwerkstatus zu erkennen.

    Netzwerkstatus:
    Über die statische Methode NetworkInterface.GetIsNetworkAvailable() bekommt man nun die Information ob die Verbindung im Moment verfügbar ist oder nicht.
    Die Klasse NetworkChange stellt hingegen das Event NetworkAddressChanged bereit, das gefeuert wird sobald sich der Netzwerkstatus ändert.

    DataBinding:
    Über DataBinding können wir nun recht einfach die Funktionalität im UserInterface darstellen.
    Mithilfe von einem Binding von der Klasse zu einer CheckBox auf das Property IsChecked=”{Binding Path=IsOnline, Source={StaticResource State}}” kann nun in XAML an eine Ressource angebunden werden. (in dem Fall wird das Property IsOnline abgegriffen von der Resource mit dem Key State)

    image 

    Somit haben wir im UserInterface (= unser Silverlight UserControl) 0 Zeilen Programmcode.

    Klasse:

    	public class NetworkState : INotifyPropertyChanged
        {
            public NetworkState()
            {
                NetworkChange.NetworkAddressChanged +=
                       (sender, args) => SendNotifyPropertyChanged("IsOnline");
            }
    
            public void SendNotifyPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
    
            public bool IsOnline
            {
                get
                {
                    return NetworkInterface.GetIsNetworkAvailable(); ;
                }
            }
            
            #region INotifyPropertyChanged Members
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            #endregion
        }

    UserControl (XAML):

    <UserControl x:Class="SilverlightNetworkState.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:local="clr-namespace:SilverlightNetworkState"
        Width="400" Height="300">
        <UserControl.Resources>
            <local:NetworkState x:Key="State" />
        </UserControl.Resources>
        <Grid x:Name="LayoutRoot" Background="White">
            <CheckBox Margin="10" IsChecked="{Binding Path=IsOnline, Source={StaticResource State}}" Content="Online" />
        </Grid>
    </UserControl>

    Download: SilverlightNetworkState 07042009.zip

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://bgrojer.spaces.live.com/blog/cns!9D87CECB1EA8118F!575.trak
    Weblogs that reference this entry
    • None