Maui android TapGestureRecognizer problem issue

Haviv Elbsz 2,151 Reputation points
2023-02-19T10:28:19.2833333+00:00

Hello all. the problem is in the this section InputTB.Text = "''; not work inside task but work outside task pleas what wrong thank you . Finally I change to bool answer = await DisplayAlert() if(answer) {...} and that works ok but I still curious why in the task no works

      answer.ContinueWith(async task =>
      {
          if (task.Result)
          {
              saveForAWhile += InputTB.Text; // this not work
              InputTB.Text = " "; // this not work
          }
      });


 <Label HorizontalOptions="Center" VerticalOptions="End"
     FontAttributes="Bold" Text="Puzzle Window" >

    <Label.GestureRecognizers>
           <TapGestureRecognizer Tapped="OnTapClearPuzzleWindow"
                   NumberOfTapsRequired="2" />
     </Label.GestureRecognizers>

 </Label>



     <Label x:Name="Open"  Text="Open" FontAttributes="Bold"
         HorizontalOptions="Start" VerticalOptions="Center"
         Grid.Row="0" Grid.Column="1" >

         <Label.GestureRecognizers>
           <TapGestureRecognizer Tapped="OnTapDeletePuzzlsList"
                   NumberOfTapsRequired="2" />
           </Label.GestureRecognizers>
     </Label>



    private void OnTapDeletePuzzlsList(object sender, TappedEventArgs e)
    { 
      string msg = "Delete Puzzles List file?";  
      Task<bool> answer = DisplayAlert("", msg, "Yes", "No");
      answer.ContinueWith(async task =>
      {
          if (task.Result)
          {
              string savetime = DateTime.Now.ToString();
              string targetFile = System.IO.Path.Combine(FileSystem.Current.AppDataDirectory, "PuzzlesList.txt");
              savedPuzzlesList = "\n Puzzles List: " + savetime + "\n\n";

              File.Delete(targetFile);

              // Write the file content to the app data directory  
              using FileStream outputStream = System.IO.File.OpenWrite(targetFile);
              using StreamWriter streamWriter = new StreamWriter(outputStream);
              await streamWriter.WriteAsync(savedPuzzlesList);
          }
      });

    }



    private void OnTapClearPuzzleWindow(object sender, TappedEventArgs e)
    { 
      
      string msg = "Clear Puzzle Window?";  
      Task<bool> answer = DisplayAlert("", msg, "Yes", "No");
      answer.ContinueWith(async task =>
      {
          if (task.Result)
          {
              saveForAWhile += InputTB.Text;
              InputTB.Text = " ";
          }
      });

    }

              
    
Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

Answer accepted by question author
  1. Anonymous
    2023-02-20T02:19:50.77+00:00

    Hello,

    If you want to operate the UI control, you need to invoke this part of code in the main thread. You can use MainThread.BeginInvokeOnMainThread like following code.

    answer.ContinueWith(async task =>
            {
                if (task.Result)
                {
                    MainThread.BeginInvokeOnMainThread(() => {
                        saveForAWhile += InputTB.Text;
                        InputTB.Text = " ";
                    });
                 }
            });
    

    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' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.