Silverlight中常用知识总结

来源:互联网 发布:软件无线电 微盘 编辑:程序博客网 时间:2024/06/06 08:54
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;            }        }...