0

I'm trying to make my longlistselector to navigate to different pages, according to the option chosen. For example A navigates to a.xaml B navigates to b.xaml, same with C D E etc.

Xaml

<phone:LongListSelector
                SelectionChanged="SelectionChanged"
  x:Name="AddrBook"
  JumpListStyle="{StaticResource AddrBookJumpListStyle}"
  Background="Transparent"
  GroupHeaderTemplate="{StaticResource AddrBookGroupHeaderTemplate}"
  ItemTemplate="{StaticResource AddrBookItemTemplate}"
  LayoutMode="List"
  IsGroupingEnabled="true"
  HideEmptyGroups ="true" Foreground="Black" Margin="0,20,0,0" FontSize="32"/>

Code Behind:

{
    public partial class ASectionPopSpanish : PhoneApplicationPage
    {
        public ASectionPopSpanish()
        {
            InitializeComponent();

            List<AddressBook> source = new List<AddressBook>();
            source.Add(new AddressBook("Joe", "Davidbisbal"));
            source.Add(new AddressBook("AdJoe", "Davidbisbal"));
            source.Add(new AddressBook("BJoe", "Davidbisbal"));
            source.Add(new AddressBook("VJoe", "Davidbisbal"));
            source.Add(new AddressBook("VJoe", "Davidbisbal"));
            source.Add(new AddressBook("CJoe", "Davidbisbal"));
            source.Add(new AddressBook("EJoe", "Davidbisbal"));
            source.Add(new AddressBook("Joe", "Davidbisbal"));

            List<AlphaKeyGroup<AddressBook>> DataSource = AlphaKeyGroup<AddressBook>.CreateGroups(source,
                System.Threading.Thread.CurrentThread.CurrentUICulture,
                (AddressBook s) => { return s.LastName; }, true);
            AddrBook.ItemsSource = DataSource;


        }

        public class AddressBook
        {
            public string LastName
            {
                get;
                set;
            }
            public string SectionPage
            {
                get;
                set;
            }

            public AddressBook(string lastname, string sectionpage)
            {
                this.LastName = lastname;
                this.SectionPage = sectionpage;
            }
        }

        private void SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                AddressBook selectedAddress = (AddressBook)AddrBook.SelectedItem;
            if (AddrBook.SelectedItem == null)
                return;
            NavigationService.Navigate(new Uri(AddressBook.SelectedItem.SectionPage & ".xaml", UriKind.Relative));
            AddrBook.SelectedItem = null;
            }
    }
}

I understand I need to implement a selectionchanged, but can't figure out how to do it, can someone give me a hand please?

Snake-leaf
  • 7
  • 1
  • 4
  • 2
    Duplicate of http://stackoverflow.com/questions/20809047/wp8-longlistselector-navigating-to-different-pages – venerik Jan 28 '14 at 07:13

1 Answers1

0

What seems missing from your snippet is, XAML/code to attach selector_SelectionChanged method to SelectionChanged event of LongListSelector :

<phone:LongListSelector
              SelectionChanged="selector_SelectionChanged"
              x:Name="AddrBook"
              .....
              ...../>

In addition, we need to cast LLS's SelectedItem to specific type to be able to access complete set of it's properties :

AddressBook selectedAddress = (AddressBook)AddrBook.SelectedItem;
NavigationService.Navigate(new Uri(selectedAddress.SectionPage + ".xaml", UriKind.Relative));
har07
  • 88,338
  • 12
  • 84
  • 137
  • thanks, I added it already but for some reason doesn't work, it gives me error, leaving the code like that, doesn't recognize the selecteditem – Snake-leaf Jan 28 '14 at 07:49
  • by removing this part `SelectionChanged="selector_SelectionChanged"`, `selector_SelectionChanged` method won't get executed when selected item changed. What was the error message specifically? – har07 Jan 28 '14 at 08:07
  • Error 1 'object' does not contain a definition for 'SectionPage' and no extension method 'SectionPage' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) D:\TechsVault Apps\Apps - Test in progress\Mega Ringtones HD\Mega Ringtones\Data\ASectionPopSpanish.xaml.cs 68 70 Mega Ringtones – Snake-leaf Jan 28 '14 at 08:55
  • `private void SelectionChanged(object sender, SelectionChangedEventArgs e) { if (AddrBook.SelectedItem == null) return; NavigationService.Navigate(new Uri(AddrBook.SelectedItem.SectionPage & ".xaml", UriKind.Relative)); AddrBook.SelectedItem = null; }` – Snake-leaf Jan 28 '14 at 08:56
  • try to cast SelectedItem to type AddressBook as shown in my updated answer. – har07 Jan 28 '14 at 10:29
  • @hard07 I updated how it looks now, but now doesn't recognize SelectedItem in the next line: `NavigationService.Navigate(new Uri(AddressBook.SelectedItem.SectionPage & ".xaml", UriKind.Relative));` Could be a simple error, but I really can't figure out what could be – Snake-leaf Jan 28 '14 at 19:12
  • Please, look at the answer closely. The next line should be using selectedAddress (SelectedItem that already casted to type AddressBook) – har07 Jan 28 '14 at 22:01
  • yes sorry I forgot to correct that, using it a string gives me the error: **operator cannot be applied to operands of type string and string windows phone 8** – Snake-leaf Jan 29 '14 at 06:16
  • Updated my answer. It should be `+` in C#. `&` is for VB. – har07 Jan 29 '14 at 06:19
  • It worked, I didn't realize about that, thank you so much for your help and for your patience! Next app I will do it again in VB that I know, I struggle too much trying to do it in C#, I need to learn more about C#. I really appreciate your help and would like to thank you in the Thanks section of my app, how should I name you? @har07 Sorry that I was a pain. – Snake-leaf Jan 30 '14 at 05:36
  • Not a pain at all, everybody need to start somewhere. "Mas Hanif" if you want to mention :) – har07 Jan 30 '14 at 06:33