xamarin DisplayPromptAsync删除变量

vc6uscn9  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(119)

老实说,我不知道为什么变量被设置为null。对象被设置,然后一旦我通过DisplayPromptAsync,它就会将对象设置为null。
我不知道该尝试什么,因为我从来没有遇到过这个问题。
这是一个问题的gif。一旦我进入字段并按提交,一个对象就会被重置。

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");
            }
            
        }
tez616oj

tez616oj1#

@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.

lstz6jyr

lstz6jyr2#

在DisplayPromptAsync中使用“Okay”或“OK”是否有区别?请尝试在代码中更改它。
这是默认页。DisplayPromptAsync方法:

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 = ""
);

相关问题