Application crashes on selecting an item in RadSide Drawer

2 posts, 0 answers
  1. Prava kafle
    Prava kafle avatar
    212 posts
    Member since:
    Apr 2010

    Posted 16 Sep Link to this post

    Hi,

    I am using a RadSideDrawer as shown in this link (http://docs.telerik.com/devtools/xamarin/controls/sidedrawer/sidedrawer-overview).

    I have a Grouped listview to show hierarchical data. When I select an item from side drawer for the first time, it alerts correct information, but if I try to select second item it crashes. 

    Is there any working sample on Side Drawer, can someone help me with this?

           private void M4ReportDrawer_SelectionChanged(object sender, NotifyCollectionChangedEventArgs e)
            {
                object cc = e.NewItems[0];
                string reportName =  (cc as M4DrawerItem).ReportName;
                 this.DisplayAlert("Selected item:", reportName, "OK");
                (this.Content.FindByName<RadSideDrawer>("M4ReportDrawer")).IsOpen = false;
            }

     

    Regards,

    Prava

  2. Lance | Tech Support Engineer, Sr.
    Admin
    Lance | Tech Support Engineer, Sr. avatar
    137 posts

    Posted 19 Sep Link to this post

    Hi Prava,

    The reason you're getting an exception is because the SelectionChange event also fires when an item is deselected. A deselection of the first item occurs when you tap the 2nd item.

    So your code: object cc = e.NewItems[0], will cause an exception because there is nothing in NewItems when the first item is deselelected before the second item becomes selected.

    The proper way to handle this would be to either check e.NewItems for items before requesting an item at an index. As an example, this will work for your needs:


    private void M4ReportDrawer_SelectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
         if (e.NewItems != null && e.NewItems.Count > 0)
         {
              object cc = e.NewItems[0];
              string reportName = (cc as M4DrawerItem).ReportName;
              this.DisplayAlert("Selected item:", reportName, "OK");
             (this.Content.FindByName<RadSideDrawer>("M4ReportDrawer")).IsOpen = false;
         }
    }

    Note: You can also catch items that were deselected by inspecting e.OldItems in the event handler.

    Please let us know if you have any further trouble. 

    Regards,
    Lance | Tech Support Engineer, Sr.
    Telerik by Progress
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. DevCraft banner
Back to Top