Toast Popup like Android in the UWP

Anderson Rodrigues Cavalcante 316 Reputation points
2021-12-07T19:34:30.317+00:00

How can I make a Toast Popup with the same function as Android? Showing and hiding automatically.

Like this image:
155649-image.png

Developer technologies | Universal Windows Platform (UWP)
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,686 Reputation points
    2021-12-07T21:52:48.047+00:00

    You can use a Popup and a StoryBoard, like :

    155745-popup.gif

    if (!StandardPopup.IsOpen)  
    {   
        StandardPopup.IsOpen = true;  
        var da = new DoubleAnimation  
        {  
            From = 0.0,  
            To = 1.0,  
            Duration = new Duration(TimeSpan.FromSeconds(2))  
        };  
        var sb = new Storyboard  
        {  
            Duration = TimeSpan.FromSeconds(4),  
            AutoReverse = true,  
        };  
        sb.Children.Add(da);  
        Storyboard.SetTarget(da, StandardPopup);  
        Storyboard.SetTargetProperty(da, "Opacity");  
        sb.Completed += delegate {if (StandardPopup.IsOpen) { StandardPopup.IsOpen = false; } };  
        sb.Begin();  
    }  
    

    XAML (change colors, location, size...):

        <Popup VerticalOffset="20" HorizontalOffset="200" x:Name="StandardPopup">  
            <Border BorderBrush="{StaticResource ApplicationForegroundThemeBrush}"  
                    CornerRadius="25"                
                Background="Gray"  
                BorderThickness="2" Width="200" Height="100">  
                <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">  
                    <TextBlock Text="This is a Popup" FontSize="24" HorizontalAlignment="Center"/>  
                </StackPanel>  
            </Border>  
        </Popup>  
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2021-12-08T01:30:31.253+00:00

    Hello,

    Welcome to Microsoft Q&A!

    In addition to @Castorix31 's answer, there is another way that you could get a similar behavior. You could take a look at InAppNotification from the Windows Community Toolkit which offers the ability to show local notifications in your application. This control should be placed where you want your notification to be displayed in the page, generally in the root grid. You could call the Dismiss method to make the notification disappear after some time.

    You could refer to my answer here: Android Like Toast Message in UWP for more information. And also, you could preview it by downloading the Windows Community Toolkit Sample App in the Windows Store. And you could also check the source code in GitHub: WindowsCommunityToolkit GitHub.

    Thank you.


    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.

    1 person found this answer helpful.
    0 comments No comments

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.