Home > front end >  DisplayPromptAsync deleting variables
DisplayPromptAsync deleting variables

Time:11-04

To be honest, I have no idea why the variable is being set to null. The object is set, then once I go through the DisplayPromptAsync, it sets the object to null.

I'm not sure what to try as I've never come across this issue.

Here's a gif of the issue. Once I enter into the field and press submit, an object gets reset. enter image description here

async void OpenContainerItem()
    {
        // Pause the timer
        blnTimerActive = false;
        if (SelectedItem != null)
        {
            if (SelectedItem.intQuanChecked == SelectedItem.intQuantity)
                return;
            try
            {
                int intQuantity = 0;
                // Ask for quantity
                string result = await Application.Current.MainPage.DisplayPromptAsync("Quantity",
                "How many "   SelectedItem.objItem.strItemName   " did you count?",
                "Okay", cancel: "Cancel",
                placeholder: "Quantity",
                keyboard: Keyboard.Numeric);

                // Check if it's been cancelled
                if (result != null)
                {
                    // check if theres nothing entered
                    if (result == "")
                    {
                        // Why tho?
                        await Application.Current.MainPage.DisplayAlert("Error", "Please enter a quantity.", "Okay");
                    }
                    else
                    {
                        intQuantity = int.Parse(result);

                        if (0 > ((SelectedItem.intQuantity - SelectedItem.intQuanChecked) - intQuantity))
                        {
                            Application.Current.MainPage.DisplayAlert("Error", "Thats too many items!", "Okay");
                            Reload();
                            blnTimerActive = true;
                            return;
                        }

                        modDatabaseUtilities.ContainerItemsPreCheck(SelectedItem.intContainerItemID, intQuantity, strCurrentUser);
                        Reload();
                    }
                }
            }
            catch(Exception ex)
            {
                Application.Current.MainPage.DisplayAlert("Error", "Couldn't process this change. Please try again.", "Okay");
            }
            
        }

CodePudding user response:

@ScottUphus - Is SelectedItem bound to a ListView? (If it is bound to anything, you should add that xaml in your question.) If so, then its a common problem: xamarin sets it back to null when the display layout is refreshed. (I'm not sure the exact "rule" for when it happens. In your case, I suspect the modal interaction causes this.)

This is how I solve such issues:

public MyItemType ValidSelectedItem { get; private set; }

public MyItemType SelectedItem
{
  get => _SelectedItem;
  set {
      ... your current setter code here ...
      // Remember most recent non-null value.
      if (value != null)
        ValidSelectedItem = value;
    }
}
private MyItemType _SelectedItem;

ValidSelectedItem remembers the non-null item, even if xamarin resets the selection back to null. Use it in code that needs that value.

CodePudding user response:

Is there a difference in using "Okay" or "OK" in DisplayPromptAsync ? try to change it in your code.

this is default Page.DisplayPromptAsync Method:

public System.Threading.Tasks.Task<string> DisplayPromptAsync 
(
 string title,
 string message,
 string accept = "OK",
 string cancel = "Cancel",
 string placeholder = default,
 int maxLength = -1,
 Xamarin.Forms.Keyboard keyboard = default,
 string initialValue = ""
);
  • Related