Property not being set

-- -- 922 Reputation points
2023-09-25T02:26:21.6533333+00:00

Hi

In my MAUI app I am calling OnItemTapped method from a command.

I have implemented IsBusy logic in OnItemTapped to avoid it being executed again until it is done. At the end of OnItemTapped method I am setting IsBusy to false.

However IsBusy remains true for some reason. This means OnItemTapped will not execute except for the first time.

What am I doing wrong and how can I get IsBusy to become false again at the end of execution of OnItemTapped?

Thanks

Regards

bool _isBusy = false;
public bool IsBusy 
{
  get =>_isBusy;
  set =>SetProperty(ref _isBusy, value);
}

public HomeViewModel() 
{
  IsBusy = false;

  ItemTappedCommand = new Command < BookedJobs > (OnItemTapped);
}

private async void OnItemTapped(BookedJobs job) {
  if (IsBusy) 
  {
    return;
  }

  IsBusy = true;

  await Shell.Current.GoToAsync($ "{typeof(BookedJobsDetailsPage).FullName}", new Dictionary < string, object > { ["BookedJobs1"] = job
  });

  IsBusy = false;  // <== Busy set to false but it does not seem to work
}

protected bool SetProperty < T > (ref T backingStore, T value, [CallerMemberName] string propertyName = "", Action onChanged = null) {
  if (EqualityComparer < T > .Default.Equals(backingStore, value)) return false;

  backingStore = value;
  onChanged ? .Invoke();
  OnPropertyChanged(propertyName);
  return true;
}

protected void OnPropertyChanged([CallerMemberName] string propertyName = "") {
  var changed = PropertyChanged;
  if (changed == null) return;

  changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,417 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,850 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 74,641 Reputation points Microsoft Vendor
    2023-09-26T03:51:15.25+00:00

    Hello,

    However IsBusy remains true for some reason. This means OnItemTapped will not execute except for the first time.

    I can reproduce this issue.

    Because Navigation was executed multiple times before IsBusy = false; , I edited your OnItemTapped method. After executing navigation, please wait for this navigating finished, then set IsBusy = false;.

    private async void OnItemTapped(BookedJobs job)
        {
            if (IsBusy)
            {
                return;
            }
           IsBusy = true;
            try
            {          
                await Shell.Current.GoToAsync($"{typeof(BookedJobsDetailsPage).FullName}", new Dictionary < string, object > { ["BookedJobs1"] = job
      });
            }
            finally
            {
                await Task.Delay(500);
                IsBusy = false;  // <== Busy set to false but it does not seem to work
            }
       }
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.