Windows Phone 数据绑定
培训第五章 数据存储 网络 推送 数据存储 临时数据存储 每一个页面都有一个状态字典 用来存储页面的临时数据 定义在PhoneApplicationPage中 每一个程序都有一个状态字典 存储程序的一些状态, 定义在PhoneApplicationService 持久数据存储 持入数据存储介绍 IsolatedStorageSettings IsolatedStorageSettings在独立存储中 Dictionary<TKey,TValue> 使用步骤: 1: 获取一个IsolatedStoragedSettings实例 IsolatedStoragedSettings MySetting = IsolatedStorageSettings.ApplicationSettings; 2: MySetting["name"] = this.name.Text; MySetting.Save(); 3: 读取数据 name.Text = MySetting["name"] as string; age.Text = MySetting["age"] as string; IsolatedStorageFile 表示包含文件和目录 涉及到的一些其它类 StreamWriter---写入 StreamReader---读取 DataContext(本地数据库) Windows Phone中使用的数据库为SQL Linq DataContext代表一个数据库对象 使用LinQ to Sql进行数据的存储和访问 数据库的操作 创建数据库 插入,查询,更新,删除 拷贝工程中的文件至独立存储 其它 网络 推送 培训第八章 依赖属性 数据绑定 依赖属性 依赖属性的引入 依赖属性的定义和使用 附加属性 IS DP AddOwner Canvas.Left/Grid.Row等 属性继承 绑定 绑定介绍 含义:一种与数据进行交互的简单方法 涉及到四个内容 绑定源 绑定目标 绑定对象 转换器 数据流向 绑定有Mode属性,它决定了数据的流向 OneTime OneWay TwoWay ElementName和绑定路径 ElementName binding 通过ElementName指定绑定数据 Path Variant 绑定到绑定源的某个属性 Source绑定 DataContext绑定 定义在FrameworkElement 可以动态改变DataContent 绑定转换器 属性改变通知
<!-- LayoutRoot 是包含所有页面内容的根网格 --> <Grid x:Name="LayoutRoot" Background="Transparent"> <!-- 效果没有显示出来 --> <!-- <Slider Margin="0,0,0,369" FontSize="30" Foreground="Red" Maximum="100" Value="30" /> <TextBlock Margin="10,216,-10,324" FontSize="30" Text="{Binding ElementName=slider, Path=Value, Converter={StaticResource DataConverter1}, ConverterParameter='1'}" /> <TextBlock Margin="10,307,-10,233" FontSize="30" Text="{Binding ElementName=slider, Path=Value, Converter={StaticResource DataConverter1}, ConverterParameter='2'}" /> --> <!-- <StackPanel x:Name="StackPanel1"> <TextBlock Text="{Binding Name}" /> <TextBlock Text="{Binding Sex}" /> <TextBlock Text="{Binding Location}" /> </StackPanel> <Button HorizontalAlignment="Center" VerticalAlignment="Top" Click="Button_Click" Content="Next" /> --> <!-- <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Slider x:Name="slider1" Grid.Row="0" Maximum="100" Value="30" /> <TextBlock Grid.Row="1" Text="{Binding ElementName=slider1, Path=Value}" /> <StackPanel Grid.Row="2" Orientation="Horizontal"> <TextBox x:Name="myName" Width="200" Text="xxd" /> <TextBlock Text="Binding variant1:" /> <TextBlock Text="{Binding ElementName=myName, Path=Text}" /> </StackPanel> </Grid> --> <!-- <Grid x:Name="ContentPanel" Grid.Row="0" Margin="12,0,12,0"> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <TextBlock x:Name="textBlock1" Grid.Row="0" Text="To be BindTarget" /> <StackPanel Grid.Row="1" VerticalAlignment="Top" Orientation="Horizontal"> <Button x:Name="button1" Click="button1_Click" Content="Bind to Button1" Tag="tag1" /> <Button x:Name="button2" Click="button2_Click" Content="Bind to Button2" Tag="tag2" /> </StackPanel> </Grid> --> <!-- <Grid x:Name="ContentPanel" Grid.Row="0" Margin="12,0,12,0"> <StackPanel> <TextBlock x:Name="textBlock1" Opacity="0" Text="Link To <a href="//Page1.xaml"> Page1 </a>" /> <local:RichTextBlock Foreground="Red" Text="{Binding ElementName=textBlock1, Path=Text}" /> </StackPanel> </Grid> --> <!-- <Grid x:Name="ContentPanel" Grid.Row="0" Margin="12,0,12,0"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal"> <TextBlock Text="{Binding RelativeSource={RelativeSource Self}, Path=FontFamily}" /> <TextBlock Text=" - " /> <TextBlock Text="{Binding RelativeSource={RelativeSource Self}, Path=FontSize}" /> <TextBlock Text=" pixels" /> </StackPanel> </Grid> --> <Grid x:Name="ContentPanel" Grid.Row="0" Margin="12,0,12,0"> <StackPanel x:Name="stackPanel1"> <TextBlock Text="{Binding Name}" /> <TextBlock Text="{Binding Sex}" /> <TextBlock Text="{Binding Location}" /> <Button Click="Button_Click_1" Content="ChangeLocation" /> </StackPanel> </Grid> </Grid>
public partial class MainPage : PhoneApplicationPage { private List<Profile> profiles; private int index; private Profile profile1; // 构造函数 public MainPage() { InitializeComponent(); // 将 listbox 控件的数据上下文设置为示例数据 //DataContext = App.ViewModel; // 用于本地化 ApplicationBar 的示例代码 //BuildLocalizedApplicationBar(); //var binding = new System.Windows.Data.Binding() {ElementName = "slider1", Path = new PropertyPath("Value")}; //textBlock1.SetBinding(TextBlock.TextProperty, binding); //Profile profile1 = new Profile("Xlc","男","北京"); //StackPanel1.DataContext = profile1; /*profiles = new List<Profile>(); Profile profile0 = new Profile("xlc","男","男"); profiles.Add(profile0); profiles.Add(new Profile("xlc","男","北京")); profiles.Add(new Profile("xianglingchuan","女","四川")); index = 0; StackPanel1.DataContext = profile0;*/ profile1 = new Profile("Vincent","Male","Beijing"); stackPanel1.DataContext = profile1; } private void Button_Click_1(object sender, RoutedEventArgs e) { profile1.Location = "Heilongjiang"; profile1.MakeLocationChange(); } /*private void button1_Click(object sender, RoutedEventArgs e) { System.Windows.Data.Binding binding1 = new System.Windows.Data.Binding() { ElementName = "button1", Path = new PropertyPath("Content") }; textBlock1.SetBinding(TextBlock.TextProperty, binding1); } private void button2_Click(object sender, RoutedEventArgs e) { System.Windows.Data.Binding binding2 = new System.Windows.Data.Binding() { ElementName = "button2", Path = new PropertyPath("Content") }; textBlock1.SetBinding(TextBlock.TextProperty, binding2); }*/ // 为 ViewModel 项加载数据 protected override void OnNavigatedTo(NavigationEventArgs e) { } /*xprivate void Button_Click(object sender, RoutedEventArgs e) { index++; if (index >= profiles.Count) index = 0; StackPanel1.DataContext = profiles[index] as Profile; }*/ }