这一节,我们要做的是把设置界面写好。不好以为那么容易,因为涉及到ListPicker的数据绑定,而且数据是从数据库里面查询获取的,所以并不是拖连个控件就可以完成的事,不过,拖控件不是件好事,要尽量避免。
<shell:ApplicationBarIconButton IconUri="/Icons/appbar.feature.settings.rest.png" Text="设置"/>
现在还有一个重要的事情要做的是修改Icon的文件生成属性。操作方法:解决方案资源管理器选中那个Settings图标文件---属性---生成操作---改为:内容(Content)。接着,运行。是否已经看到图标了呢。。。
private void ApplicationBarIconButton_Click(object sender, EventArgs e) { NavigationService.Navigate(new Uri("/WeatherForecast;component/SetPage.xaml", UriKind.Relative)); }
点击运行。是不是发现现在点击那个IconButton可以导航到SetPage页面了呢。
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <Grid.RowDefinitions> <RowDefinition Height="100"/> <RowDefinition Height="100"/> <RowDefinition Height="100"/> <RowDefinition Height="100"/> </Grid.RowDefinitions> <TextBlock Text="省份:" Grid.Row="0" FontSize="40" HorizontalAlignment="Left" VerticalAlignment="Bottom"/> <toolkit:ListPicker x:Name="provincelp" Grid.Row="1" SelectionChanged="province_SelectionChanged" /> <TextBlock Text="城市:" Grid.Row="2" FontSize="40" HorizontalAlignment="Left" VerticalAlignment="Bottom"/> <toolkit:ListPicker x:Name="citylp" Grid.Row="3" SelectionChanged="city_SelectionChanged"/> </Grid>
先给Provincelp添加一个SelectionChange事件。
string[] prov = { "北京", "上海", "天津", "重庆", "黑龙江", "吉林", "辽宁", "内蒙古", "河北", "山西", "陕西" }; private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) { //给省份的listpicker绑定数据 provincelp.ItemsSource = prov; } private void provincelp_SelectionChanged(object sender, SelectionChangedEventArgs e) { ListPicker lp = sender as ListPicker; string p = lp.SelectedItem.ToString(); cityDataBind(p); } /// <summary> /// 由当前选择的省份给city的ListPicker绑定数据 /// </summary> /// <returns></returns> void cityDataBind(String prov) { IList<CityInfoTable> list = null; using (CityDataContext db = new CityDataContext(CityDataContext.connectionString)) { IQueryable<CityInfoTable> queries = from c in db.CityInfos where c.Province == prov select c; list = queries.ToList(); } List<String> l = new List<string>(); foreach (var item in list) { l.Add(item.CityName); } citylp.ItemsSource = l; }
运行,成功!