Forum Xamarin.Forms

Display Alert within context action needs a cast type?

I'm working on a lab for a class, the final step is adding a context button to the listview we've created in a previous step. I'm trying to get an alert to pop up when the button has been pressed, but I kept getting an error. I finally copied a bit of code from the instructor but after running it I'm getting an invalid cast type. Can someone explain both how to fix the issue, and why a cast type is necessary?

Code before instructor:

var button = new Button //ListButton // Android workaround
                {
                    Text = "Buy Now",
                    BackgroundColor = Color.Orange,
                    FontSize = 12,
                    HorizontalOptions = LayoutOptions.EndAndExpand
                };
                button.SetBinding(Button.CommandParameterProperty, new Binding("."));
                button.Clicked += async (sender, e) =>
                {
                    var b = (Button)sender;
                    var item = (ListItem)b.CommandParameter;
                    await DisplayAlert("Error", "You must first select a payment option", "OK");
                    if (info.getName() == "")
                    {

                        await ((ContentPage)b.Parent).DisplayAlert("Error", "You must first select an item", "OK");
                    }
                    else if (info.getPaymentSelected() == 0)
                    {
                        await DisplayAlert("Error", "You must first select a payment option", "OK");
                    }
                    else
                    {
                        await DisplayAlert("Order Summary", "Payment Processing", "OK");
                    }

                };

Code after instructor:

var button = new Button //ListButton // Android workaround
                {
                    Text = "Buy Now",
                    BackgroundColor = Color.Orange,
                    FontSize = 12,
                    HorizontalOptions = LayoutOptions.EndAndExpand
                };
                button.SetBinding(Button.CommandParameterProperty, new Binding("."));
                button.Clicked += async (sender, e) =>
                {
                    var b = (Button)sender;
                    var item = (ListItem)b.CommandParameter;
                    await ((ContentPage)((Grid)((ListView)((StackLayout)b.Parent).Parent).Parent).Parent).DisplayAlert("Error", "You must first select a payment option", "OK");
                    if (info.getName() == "")
                    {

                        await ((ContentPage)b.Parent).DisplayAlert("Error", "You must first select an item", "OK");
                    }
                    else if (info.getPaymentSelected() == 0)
                    {
                        await ((ContentPage)((ListView)((StackLayout)((StackLayout)b.Parent).Parent).Parent).Parent).DisplayAlert("Error", "You must first select a payment option", "OK");
                    }
                    else
                    {
                        await ((ContentPage)((ListView)((StackLayout)((StackLayout)b.Parent).Parent).Parent).Parent).DisplayAlert("Order Summary", "Payment Processing", "OK");
                    }

                };

Best Answer

Answers

  • NMackayNMackay GBInsider, University admin
     await ((ContentPage)((Grid)((ListView)((StackLayout)b.Parent).Parent).Parent).Parent).DisplayAlert("Error", "You must first select a payment option", "OK");
    

    The instructor told you to do that? :*

    Should be safe with

    await Application.Current.MainPage.DisplayAlert
    

    Used that approach in dialog services before with no problem. John's covered all the scenarios.

  • JohnHardmanJohnHardman GBUniversity admin

    @SidneySmi

    Actually, I've just noticed that your original "Code before instructor:" contained

    await DisplayAlert("Error", "You must first select a payment option", "OK");
    

    If that compiles, then you are already in the context of a Page. In that case, all of your calls to DisplayAlert should be done this way, without any extra work to identify a Page. Only if you are not in the context of a Page already should you do any of the options I listed above. You certainly shouldn't be doing all those horrible casts from the "Code after instructor:"

  • Ok so


    worked like a charm. I was really confused from the instructors code because it was basically just from his example app. I knew it wouldn't work with mine because he used stack layouts and I used grid layout. However I couldn't find the right combination to work. If I just put display alert without any of the parent stuff it wouldn't show me the exception and it also wouldn't give me an error line. However when I put the parent line in it would at least show the 3 alert lines as the exception errors.

  • JohnHardmanJohnHardman GBUniversity admin

    @SidneySmi said:
    I was really confused from the instructors code because it was basically just from his example app. I knew it wouldn't work with mine because he used stack layouts and I used grid layout. However I couldn't find the right combination to work.

    IMHO, anybody who does something like the following should not only not be teaching, they should not be allowed near a keyboard:

    ((ContentPage)((Grid)((ListView)((StackLayout)b.Parent).Parent).Parent).Parent).
    
Sign In or Register to comment.