Silverlight中常用知識總結

暖楓無敵發表於2011-10-08
public abstract class NotificationObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
 
    protected virtual void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
 
    protected void RaisePropertyChanged(params string[] propertyNames)
    {
        if (propertyNames == null) throw new ArgumentNullException("propertyNames");
 
        foreach (var name in propertyNames)
        {
            this.RaisePropertyChanged(name);
        }
    }
 
   protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
    {
       var propertyName = ExtractPropertyName(propertyExpression);
        this.RaisePropertyChanged(propertyName);
    }
 
    public static string ExtractPropertyName<T>(Expression<Func<T>> propertyExpression)
   {
        if (propertyExpression == null)
        {
           throw new ArgumentNullException("propertyExpression");
        }
 
        var memberExpression = propertyExpression.Body as MemberExpression;
        if (memberExpression == null)
        {
            throw new ArgumentException("PropertySupport_NotMemberAccessExpression_Exception", "propertyExpression");
        } 
        var property = memberExpression.Member as PropertyInfo;
        if (property == null)
       {
            throw new ArgumentException("PropertySupport_ExpressionNotProperty_Exception", "propertyExpression");
        }

       var getMethod = property.GetGetMethod(true);
        if (getMethod.IsStatic)
       {
           throw new ArgumentException("PropertySupport_StaticExpression_Exception", "propertyExpression");
        }
 
        return memberExpression.Member.Name;
    }
}

相應的,Student型別為:
public class Student : NotificationObject
{
    string firstName;
    public string FirstName
    {
       get
        {
           return firstName;
        }
        set
        {
            firstName = value;
            //Notify("FirstName");
            this.RaisePropertyChanged("FirstName");
        }
   }
   string lastName;
    public string LastName
    {
        get
       {
            return lastName;
        }
        set
        {
            lastName = value;
          //Notify("LastName");
            this.RaisePropertyChanged("LastName");
        }
    }
 
    public Student(string firstName, string lastName)
    {
        this.firstName = firstName;
        this.lastName = lastName;
    }
   
}


Windows Phone7獲取當前網路狀態

using Microsoft.Phone.Net.NetworkInformation;
...

        string netState, netName;
        private bool _networkIsAvailable;
        private NetworkInterfaceType _currentNetworkType; //網路連線的型別    
        private void GetNetInfo(object sender, RoutedEventArgs e)
        {
            _networkIsAvailable = NetworkInterface.GetIsNetworkAvailable();//當前網路是否可用
            _currentNetworkType = NetworkInterface.NetworkInterfaceType;//獲取當前網路的型別

            if (_networkIsAvailable)
            {
                netState = "聯網狀態";
                //Message.Background = new SolidColorBrush(Colors.Green);
            }  
            else
            {
                netState = "斷網狀態";
                //Message.Background = new SolidColorBrush(Colors.Red);
            } 

            switch (_currentNetworkType)
            {
                case NetworkInterfaceType.MobileBroadbandCdma:
                    netName = "CDMA網路";
                    break;
                case NetworkInterfaceType.MobileBroadbandGsm:
                    netName = "CSM網路";
                    break;
                case NetworkInterfaceType.Wireless80211:
                    netName = "Wi-Fi網路";
                    break;
                case NetworkInterfaceType.Ethernet:
                    netName = "Ethernet網路";
                    break;
                case NetworkInterfaceType.None:
                    netName = "網路不可用";
                    break;
                default:
                    netName = "其他的網路";
                    break;
            }
        }
...


相關文章