继续《在WindowsPhone8中生成基于MVVM Light的LongListSelector拼音检索绑定》一文,项目不再引用上一个项目,但功能实现相同,我们已经对数据作了绑定,下面要携带参数传值导航至具体内容页:
1.在引用中添加
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP71"
2.绑定命令:我实现的功能为:单机LongListSelector的某个项(此例中为文本框),跳转至相应页面(ContentPage)内容页
<phone:LongListSelector x:Name="MountainIndexLLS" JumpListStyle="{StaticResource AddrBookJumpListStyle}" Background="Transparent" GroupHeaderTemplate="{StaticResource AddrBookGroupHeaderTemplate}" LayoutMode="List" IsGroupingEnabled="true" HideEmptyGroups ="true" ItemsSource="{Binding DataSource}"> <phone:LongListSelector.ItemTemplate> <DataTemplate> <StackPanel VerticalAlignment="Top"> <TextBlock FontWeight="Bold" Text="{Binding Name}" Style="{StaticResource PhoneTextLargeStyle}" FontFamily="{StaticResource PhoneFontFamilySemiBold}" Foreground="White"/> <TextBlock Text="{Binding Region}" Style="{StaticResource PhoneTextNormalStyle}" TextWrapping="Wrap" FontFamily="{StaticResource PhoneFontFamilySemiBold}" Foreground="Gray"/> </StackPanel> </DataTemplate> </phone:LongListSelector.ItemTemplate> <i:Interaction.Triggers> <i:EventTrigger EventName="Tap"> <cmd:EventToCommand Command="{Binding TapCommand}" CommandParameter="{Binding SelectedItem,ElementName=MountainIndexLLS}"/> </i:EventTrigger> </i:Interaction.Triggers> </phone:LongListSelector>
在这里,我们将上例中的LongListSelector中LongListSelector.ItemTemplate的样式代码由PhoneApplicationPage.Resources中转移到了LongListSelector中,在这里,每一个LongListSelector项均被绑定了一个TapCommand的命令,下面,我们在MainViewModel中完成这条命令,实现我们需要的功能。
3.写命令代码:(MainViewModel)
public RelayCommand<MountainIndex> TapCommand { get; private set; } public void NavigationTap(MountainIndex mouIndex) { System.Windows.MessageBox.Show(mouIndex.Name+mouIndex.Region); }
我们先不实现导航,仅仅是取得文本框的内容
public MainViewModel() { TapCommand = new RelayCommand<MountainIndex>((mouIndex) => NavigationTap(mouIndex)); }
实现结果如下: