Silverlight 学习笔记之【一】

一、 XamlReader 动态加载 Silverlight 对象 。

       在使用XamlReader.Load 前,要先引用 System.Windows.Markup 命名空间。

 

注意几点:

1.被XamlReader.Load 加载的单个XAML 内容 必须包含XAML 的命名空间,常用的是http://schemas.microsoft.com/client/2007,但JS编程模型中用CreateFromXAML 创建 Silverlight 对象时可以省略这个命名空间,因为Silverlight 会隐式声明它。

2.XAML 文件中必须存在 Silverlight 根元素的内容 ,不能够使用XamlReader.Load 替换整个XAML 树。

3.使用 XamlReader.Load 创建的内容只能赋予一个Silverlight 对象,它们是一对一的关系。

4.可以使用XamlReader.Load 创建的对象添加的对象必须是一个可以容纳该子元素的Silverlight 对象。

 

如,创建一个TextBlock 对象:

StringBuilder xaml=new StringBuilder();

xaml.Append("<TextBlock ");

xaml.Append("xmlns=/"http://schemas.microsoft.com/client/2007/" ");

xaml.Append("Canvas.Left=/"50/" Canvas.Top=/"30/" FontSize=/"50/" ");

xmal.Append(" FontWeight=/"Bold/" Text=/"动态创建XAML对象/" /> ");

 

TextBlock textBlock=(TextBlock) XamlReader.Load(xaml.ToString());

 

 

 

二、 使用 DispatcherTimer 计时器

      DispatcherTimer 是Silverlight 中十分有用的一个计时器对象,使用DispatcherTimer 是十分简单的,只需要为DispatcherTimer 设置一个间隔时间,然后创建Tick 的事件处理, 当执行Start() 方法时,即开始计时后,Tick事件就会根据你设置的间隔时间来执行事件处理中的处理。

        使用计时器对象前,需要引用 System.Widnows.Threading 命名空间。

 

 

三、 Silverlight 全屏支持

      在Silverlight 中使用 全屏功能,需要在托管代码类中引入 System.Widnows.Interop  命名空间。

如:

    private void Button_Click(object sender,RoutedEventArgs e)

{

        //获取当前应用程序 Silverlight 内容对象

        Content  contentObject=Application.Current.Host.Content;

        contentObject.IsFullScreen=! contentObject.IsFullScreen;

}

如果 一个网面中同时存在多个Silverlight 应用程序 ,那么 同一时间只有一个Silverlight 应用程序可以处于全屏显示模式。

 

 

四、数据绑定

     数据绑定是数据源和目标数据之间的一座桥梁,在Silverlight 中,这座桥梁有三个方向:

     1.OneWay : 单向,仅从数据源绑定到目标。

     2.TwoWay : 双向,即可以从数据源绑定绑定到目标,目标的更改也可以反馈给数据源,使其发生更新。

     3.OneTime : 可以说它是OneWay 的一个特例,表示仅从数据源加载一次数据,这样做可以带来性能 上的提升。

如:

XAML:

    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel>
            <TextBlock Text="{Binding Title,Mode=OneWay }" Name="Title"></TextBlock>
            <TextBlock Text="{Binding Price,Mode=TwoWay }" Name="Price"></TextBlock>
        </StackPanel>
        <Button Name="MyButton" Content="修改" Click="MyButton_Click" Width="40" Height="20"></Button>
    </Grid>


后台代码:

        public MainPage()
        {
            InitializeComponent();
            book.Title = "ABC";
            book.Price = 30;
            Title.DataContext = book;
            Price.DataContext = book;
        }

        Book book = new Book();

        private void MyButton_Click(object sender, RoutedEventArgs e)
        {
            Random rand = new Random();
            book.Price = 50 + rand.Next(400);
           
        }



    public class Book : INotifyPropertyChanged
    {
        private string _title;
        public string Title
        {
            get
            {
                return _title;
            }
            set
            {
                _title = value;
            }
        }

        private double _price;
        public double Price
        {
            get { return _price; }
            set {
                _price = value;
                NotifyPropertyChange ("Price");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged ;

        private void NotifyPropertyChange (string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                    new PropertyChangedEventArgs (propertyName));
            }
        }

        #endregion
    }

 

Book 类实现了INotifyPropertyChanged 接口的 PropertyChanged 事件 ,并对Price 属性添加监视。

使用 INotifyPropertyChanged 接口时,需要 引入 System.ComponentModel 命名空间。

 

 

五、数据验证

 

要实现数据验证,必须将绑定对象上的 ValidatesOnExceptions(是通知绑定引擎在发生异常时创建验证错误) 属性 和 NotifyOnValidationError(是在发生异常时引发BindingValidationError) 属性设置为 true 。

如:

XAML:

<Grid x:Name="LayoutRoot" Background="White" ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" Margin="5" Grid.Row="0">
            <TextBlock x:Name="Title" FontSize="15" Height="30" Text="{Binding Title,Mode=OneWay}"></TextBlock>
            <TextBlock x:Name="Price" FontSize="15" Height="30" Text="{Binding Price,Mode=TwoWay}"></TextBlock>
            <TextBox x:Name="MyTextBox" Margin="5" Width="100" Height="30" FontSize="15" Text="{Binding Price,Mode=TwoWay,NotifyOnValidationError=True,ValidatesOnExceptions=True }"
                     BindingValidationError ="MyTextBox_BindingValidationError"></TextBox>
        </StackPanel>
        <Button x:Name="MyButton" Content="修改" Click="MyButton_Click" FontSize="15" Width="80" Height="30" Grid.Row="1"></Button>
    </Grid>

 

 

Book.cs:

    public class Book :INotifyPropertyChanged
    {

        private string _title;
        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        private double _price;
        public double Price
        {
            get { return _price; }
            set {
                if (value < 0)
                {
                    throw new Exception("单价不能为负数,请重新输入!");
                }
                _price = value;
                NotifyPropertyChanged("Price");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }

 

 

   public partial class MainPage : UserControl
    {
        Book book = new Book();

        public MainPage()
        {
            InitializeComponent();
            book.Title = "ASP.NET 开发手册";
            book.Price = 60;
            Title.DataContext = book;
            Price.DataContext = book;
            MyTextBox.DataContext = book;
        }

        private void MyTextBox_BindingValidationError(object sender, ValidationErrorEventArgs e)
        {
            if (e.Action == ValidationErrorEventAction.Added)
                MyTextBox.Background = new SolidColorBrush(Colors.Red);
            else if (e.Action == ValidationErrorEventAction.Removed)
                MyTextBox.Background =new SolidColorBrush(Colors.White);
        }

        private void MyButton_Click(object sender, RoutedEventArgs e)
        {
            book.Price = Convert.ToDouble(MyTextBox.Text);
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

春哥撩编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值