Forum Xamarin.Forms

Main thread issues with alerts

ScottVentsUSScottVentsUS Member ✭✭
edited October 2020 in Xamarin.Forms

Hey guys and gals,

I've run into an issue I don't know how to solve. Somehow my entire viewmodel is not running in the main thread like I feel like it should? (Idk if that's the right terminology or even makes sense...) Basically, I can't run "DisplayAlert" without doing the "MainThread.BeginInvokeOnMainThread()" Thingy.

Which is fine for the normal DisplayAlerts, but does not work on DisplayPromptAsync.

I don't know how I set it to be on a different thread other than the main thread. Maybe I did something stupid simple since I can't find any other threads on this specific issue of the DisplayPromptAsync issue. but ideally, if I could just have the whole thing run on the main thread, that'd be wonderful. Otherwise, how can I display a numeric input?

Here's what does not work, and where I am stuck:

do
{

            MainThread.BeginInvokeOnMainThread(() =>
            {
                // Ask for quantity
                result = await Application.Current.MainPage.DisplayPromptAsync("Question 1",
                "How many " + SelectedItem.strItemName + " do you want to pull? You need " + SelectedItem.intQuantity +
                ". The selected location contains " + intAmountAtLocation + " of the selected item.",
                "Okay", "Cancel",
                placeholder: "Quantity",
                keyboard: Keyboard.Numeric);

                if (result != null)
                {
                    intPullAmount = int.Parse(result);

                    if (intPullAmount > intAmountAtLocation)
                    {
                        Application.Current.MainPage.DisplayAlert("Error", "You can only pull " + intAmountAtLocation + " from the selected location.", "Oh, okay");
                        return;
                    }
                }
            });


        } while (intPullAmount == -1);

EDIT: Some other info that may be helpful is that in the viewmodel's constructor, it's able to run the Display alert without the invoke on main thread. But once I call the above code (from a method that was called by a command) I have to. So this is being called by cmdScanned which calls this method.

Best Answer

Answers

  • ScottVentsUSScottVentsUS Member ✭✭

    @Jarvan said:

    Which is fine for the normal DisplayAlerts, but does not work on DisplayPromptAsync.

    This is because you added await when calling the DisplayPromptAsync method, which requires the async keyword. Try adding the async to the method:

    MainThread.BeginInvokeOnMainThread(async () =>
    {
        var result = await Application.Current.MainPage.DisplayPromptAsync(...);
        ...
    });
    

    I just tried this and no luck.. It goes through it and just doesn't display.

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    Is SelectedItem possibly null? Could be you're getting an exception in creating the prompt and that's why it's not displaying.

  • ScottVentsUSScottVentsUS Member ✭✭
    edited October 2020

    No, I'm sure this isn't the best way to do this, but I think I "fixed" it by in the command dec, I did this:

                // Commands
                cmdScanned = new Command(() =>
                {
                    MainThread.BeginInvokeOnMainThread(() =>
                    {
                        Scanned();
                    });
                });
    

    edit: Okay, I've tried a bunch of different ways to format it, I'm sorry I couldn't figure it out. I don't know how I did it earlier.. If you know how, could you let me know so I can fix it? Thanks

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    @ScottVentsUS said:
    edit: Okay, I've tried a bunch of different ways to format it, I'm sorry I couldn't figure it out. I don't know how I did it earlier.. If you know how, could you let me know so I can fix it? Thanks

    Three backticks surrounding your block of code:

    ```
    Insert code here
    ```

    turns into

    Insert code here
    

    Or `one backtick` for inline bits like this.

  • ScottVentsUSScottVentsUS Member ✭✭

    @JoeManke said:

    @ScottVentsUS said:
    edit: Okay, I've tried a bunch of different ways to format it, I'm sorry I couldn't figure it out. I don't know how I did it earlier.. If you know how, could you let me know so I can fix it? Thanks

    Three backticks surrounding your block of code:

    ```
    Insert code here
    ```

    turns into

    Insert code here
    

    Or `one backtick` for inline bits like this.

    You're awesome! Thanks! I updated my response so it's readable now haha

Sign In or Register to comment.