alexrainman / SegmentedControl

MIT License
86 stars 38 forks source link

Invalid cast exception. Android platform. #11

Closed AndriiTsok closed 7 years ago

AndriiTsok commented 7 years ago

SegmentedControlRenderer throws the exception on SegmentedControlRenderer line 140. Screenshot (debugging): http://prntscr.com/diphc0

From the demo project it works fine.

alexrainman commented 7 years ago

Are you adding the segments programmatically? First, this control requires Android 4.4 and up.

AndriiTsok commented 7 years ago

Yes I am. Our android project configured for 21+

alexrainman commented 7 years ago

Thats the problem. The control is designed to add the items programmatically. Ummm let me think about it.

AndriiTsok commented 7 years ago

Just a snippet.. how I'm trying to wotk with it.

var segmentControl = new SegmentedControl.FormsPlugin.Abstractions.SegmentedControl();
            segmentControl.Children.Add(new SegmentedControl.FormsPlugin.Abstractions.SegmentedControlOption() { Text = "ONE" });
            segmentControl.Children.Add(new SegmentedControl.FormsPlugin.Abstractions.SegmentedControlOption() { Text = "TWO" });
            segmentControl.Children.Add(new SegmentedControl.FormsPlugin.Abstractions.SegmentedControlOption() { Text = "THREE" });
            segmentControl.SelectTab(0);
            segmentControl.Margin = new Thickness(8, 0);
            var segmentedControlContainer = new StackLayout()
            {
                Orientation = StackOrientation.Vertical,
                Children = {
                    segmentControl
                },
                Spacing = 0,
                Padding = new Thickness(0, 5)
            };
alexrainman commented 7 years ago

Try adding the segments at xaml level.

AndriiTsok commented 7 years ago

The thing is that we follow declarative UI implementation in code and don't use Xaml at all. :) It still crashes even if to move adding segment options to the OnAppearing.

One workaround I found is to comment 134-144 and just use two lines 136, 137 instead. PS: Thank you for your quick response to the issue! Appreciate!

AndriiTsok commented 7 years ago

I'm going to fix it by checking and comparing types instead of using the index.

AndriiTsok commented 7 years ago

I've added pull request for the proposed fix. #12 Would be awesome if you could validate the fix.

AndriiTsok commented 7 years ago

Have just tested for both approaches XAML and declaratively. Works fine now. If you create updated NuGet package today I will use the lib. for the production app :) Thank you.

alexrainman commented 7 years ago

Give me a few min as your fix apply in one more place.

AndriiTsok commented 7 years ago

Oh sorry.. these line endings...

alexrainman commented 7 years ago

Thanks for the fix.