2

I'm currently writing a maui app in .NET 8.0 and after a method is complete, I usually display a message announcing the procedure was completed successfully, and I do this using the DisplayAlert() command.

However, what I am looking for on certain procedures is for the DisplayAlert() to close automatically after a few seconds if the OK button is not clicked.

Is there a way to do this?

If it cannot be done with DisplayAlert() is there another message box or class that is capable of this function?

4
  • 1
    to display brief popups that auto dismiss, use a Toast Commented Dec 1, 2024 at 15:45
  • Assuming the "method" doesn't run too long, and there is some "context", you can use a Tooltip by hanging one off the nearest relevant control (on the fly). learn.microsoft.com/en-us/dotnet/maui/user-interface/… Commented Dec 1, 2024 at 18:30
  • You can use the platform code. Commented Dec 2, 2024 at 1:47
  • Also CommunityToolkit.Maui's Popup Commented Dec 2, 2024 at 8:08

2 Answers 2

2

Here is my solution.

------ Update ------

Here I use the community code.

private  async  void OnCounterClicked(object sender, EventArgs e)
{        
   popup = new CommunityToolkit.Maui.Views.Popup
    {              
        Content = new VerticalStackLayout
        {
            BackgroundColor = Colors.Red,
            Children =
{
    new Label
    {
        Text = "This is a very important message!"
        ,
         BackgroundColor = Colors.Red,
    },
    new Microsoft.Maui.Controls.Grid{    
        ColumnDefinitions ={              
        new Microsoft.Maui.Controls.ColumnDefinition {
        Width=GridLength.Auto
        },
          new Microsoft.Maui.Controls.ColumnDefinition {
        Width=GridLength.Auto
        }
        },
        Children = {    
       new Microsoft.Maui.Controls.Button{
     Text = "Cancel"
    },
        }  
    }  
}
        }
    };
    this.ShowPopup(popup);
    await DelayTask();    
}

     public async Task DelayTask() {
         await Task.Delay(3000);
         popup.Close();
     }
Sign up to request clarification or add additional context in comments.

6 Comments

This looks good, but I'm running on a Windows platform. Would this still work?
You can do this by using popups, as jason said.
Have you solved the problem ?@BobGatto
No i'm haven't solved this problem yet. Right now I'm studying using a Toast. But when I set it up the way Microsoft instructs me to, my app gets an error from the Package_Appxmanifest file. @liyu
I have updated my answer ,you can give it a try.@BobGatto
|
2

Here's a reusable DisplayPopup that takes arguments similar to DisplayAlert. The VerticalStackLayout is initially set to be invisible with Opacity = 0.0, allowing FadeTo to be used for animating the Popup reveal and concealment.

async Task DisplayPopup(string title, string message, string cancel)
{
    VerticalStackLayout verticalStackLayout;
    CommunityToolkit.Maui.Views.Popup popup = new()
    {
        Content = verticalStackLayout = new VerticalStackLayout
        {
            Padding = new Thickness(20),
            Opacity = 0.0,
            Children =
            {
                new Label { Text = title, FontSize = 24 },
                new Label { Text = message, FontSize = 16 }
            }
        }
    };
    verticalStackLayout.Loaded += async (s, e) =>
    {
        await verticalStackLayout.FadeTo(1.0, 250);
        await Task.Delay(1000);
        await verticalStackLayout.FadeTo(0.0, 250);
        await popup.CloseAsync();
    };
    await this.ShowPopupAsync(popup);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.