This is a migrated thread and some comments may be shown as answers.

Application crashes on selecting an item in RadSide Drawer

1 Answer 23 Views
SideDrawer
This is a migrated thread and some comments may be shown as answers.
Prava kafle
Top achievements
Rank 1
Prava kafle asked on 16 Sep 2016, 09:24 PM

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

1 Answer, 1 is accepted

Sort by
0
Lance | Manager Technical Support
Telerik team
answered on 19 Sep 2016, 07:04 PM
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
Tags
SideDrawer
Asked by
Prava kafle
Top achievements
Rank 1
Answers by
Lance | Manager Technical Support
Telerik team
Share this question
or