Windows Phone7 开发教程 - 数据绑定

openkk 13年前
     <span style="line-height:21px;border-collapse:collapse;font-family:微软雅黑, Tahoma, Helvetica, SimSun, sans-serif, Hei;color:#444444;font-size:14px;">对于WP7应用程序来说微软使用了MVC的设计方式,通过数据绑定Data Binding技术可以轻松的将逻辑事务和UI界面连接起来。如果你学习过Silverlight或ASP.NET掌握数据绑定可以跳过本文。对于Windows Phone开发来说,一般为.xaml界面布局文件和对应的.Net代码实现比如.cs文件,我们就Windows Phone开发必须掌握的数据绑定做个简单的例子。<br /> </span>    <p><span style="line-height:21px;border-collapse:collapse;font-family:微软雅黑, Tahoma, Helvetica, SimSun, sans-serif, Hei;color:#444444;font-size:14px;"><br /> </span></p>    <p><span style="line-height:21px;border-collapse:collapse;font-family:微软雅黑, Tahoma, Helvetica, SimSun, sans-serif, Hei;color:#444444;font-size:14px;"> 一、数据模型 ViewModel 类必须实现 INotifyPropertyChanged 接口来通知UI数据的改变,关键地方代码Zune123使用了红色字体标注出来。代码如下</span></p>    <p></p>    <pre class="brush:c#; toolbar: true; auto-links: false;">[DataContract] public class ViewModel : INotifyPropertyChanged {     private string _textBox1Text;     private bool _checkBox1IsChecked;     private bool _radioButton1IsChecked;     private bool _radioButton2IsChecked;     private double _slider1Value;     public event PropertyChangedEventHandler PropertyChanged;     private void NotifyPropertyChanged(string propertyName)     {         if (null != PropertyChanged)             PropertyChanged(this, new PropertyChangedEventArgs(propertyName));     }     [DataMember]     public string TextBox1Text     {         get { return _textBox1Text; }         set         {             _textBox1Text = value;             NotifyPropertyChanged("TextBox1Text");         }     }     [DataMember]     public bool CheckBox1IsChecked     {         get { return _checkBox1IsChecked; }         set         {             _checkBox1IsChecked = value;             NotifyPropertyChanged("CheckBox1IsChecked");         }     }     [DataMember]     public double Slider1Value     {         get { return _slider1Value; }         set         {             _slider1Value = value;             NotifyPropertyChanged("Slider1Value");         }     }     [DataMember]     public bool RadioButton1IsChecked     {         get { return _radioButton1IsChecked; }         set         {             _radioButton1IsChecked = value;             NotifyPropertyChanged("RadioButton1IsChecked");         }     } }</pre>    <p></p>    <span style="line-height:21px;border-collapse:collapse;font-family:微软雅黑, Tahoma, Helvetica, SimSun, sans-serif, Hei;color:#444444;font-size:14px;">二、界面XAML关联<br /> </span>    <p><span style="line-height:21px;border-collapse:collapse;font-family:微软雅黑, Tahoma, Helvetica, SimSun, sans-serif, Hei;color:#444444;font-size:14px;">  1. TextBlock的内容和刚才ViewModel的关联,在.xaml文件中代码如下:</span></p>    <p></p>    <pre class="brush:xml; toolbar: true; auto-links: false;"><TextBox Height="72" HorizontalAlignment="Left" Margin="20,0,0,0" Name="textBox1" Text="{Binding TextBox1Text, Mode=TwoWay}" VerticalAlignment="Top" Width="440" /></pre>    <p></p>    <p><span style="line-height:21px;border-collapse:collapse;font-family:微软雅黑, Tahoma, Helvetica, SimSun, sans-serif, Hei;color:#444444;font-size:14px;"> 2. CheckBox复选控件的数据绑定:</span></p>    <p></p>    <pre class="brush:xml; toolbar: true; auto-links: false;"><CheckBox Content="CheckBox" Height="71" Name="checkBox1" Margin="20,0,0,0" IsChecked="{Binding CheckBox1IsChecked, Mode=TwoWay}"/>   </pre>    <p></p>    <p><span style="line-height:21px;border-collapse:collapse;font-family:微软雅黑, Tahoma, Helvetica, SimSun, sans-serif, Hei;color:#444444;font-size:14px;">  3. Slider滑块控件的Data Binding:</span></p>    <p></p>    <pre class="brush:c#; toolbar: true; auto-links: false;"><Slider Height="84" Name="slider1" Width="440" Margin="20,0,0,0" Value="{Binding Slider1Value, Mode=TwoWay}"/></pre>    <p></p>    <p><span style="line-height:21px;border-collapse:collapse;font-family:微软雅黑, Tahoma, Helvetica, SimSun, sans-serif, Hei;color:#444444;font-size:14px;"> 4. RadioButton单选控件的数据绑定</span></p>    <p></p>    <pre class="brush:c#; toolbar: true; auto-links: false;"><RadioButton Content="RadioButton 1" IsChecked="{Binding RadioButton1IsChecked, Mode=TwoWay}" Height="71" Name="radioButton1" GroupName="Numbers"  Margin="20,0,0,0"/></pre>    <p></p>    <p></p>    <p><span style="font-family:微软雅黑, Tahoma, Helvetica, SimSun, sans-serif, Hei;color:#444444;"><span style="line-height:21px;border-collapse:collapse;font-size:14px;"><br /> </span></span></p>    <p><span style="font-family:微软雅黑, Tahoma, Helvetica, SimSun, sans-serif, Hei;color:#444444;"><span style="line-height:21px;border-collapse:collapse;font-size:14px;">来源:</span></span><a href="/misc/goto?guid=4959500066272289226" rel="nofollow">http://bbs.obaoz.com/thread-11596-1-1.html</a></p>    <p></p>