Alert With Rg.Plugins.Popuppage

Hey Xamarians,

This is my second article on the Rg.Plugins.Popuppage plugin. With the help of this plugin, we can create a warning notification and we need to customize our type.

Xamarin

Alert

Alert shows some information or warning such as exceptions, and whether there is an open net connection or not. So it warns me whether a network connection is available or not.

Xamarin

If you have not read my last article, before proceeding with this article please Click Here.

Implementation

Open Visual Studio and open project an "XFPopupAnimation"...

Then, we are creating a page whose name is AlertPopup
  • => Go to project solution
  • => Right click on pcl project
  • => Click on "New Item" then select Page.
Here I am writing XAML code for the alert.
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <pages:PopupPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="XFPopupAnimation.AlertPopup" xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup" xmlns:animations="clr-namespace:Rg.Plugins.Popup.Animations;assembly=Rg.Plugins.Popup" BackgroundColor="Transparent" InputTransparent="True" HasSystemPadding="False" CloseWhenBackgroundIsClicked="False">  
  3.     <pages:PopupPage.Animation>  
  4.         <animations:MoveAnimation PositionIn="Top" PositionOut="Top" /> </pages:PopupPage.Animation>  
  5.     <StackLayout x:Name="Mainstk" Orientation="Horizontal" BackgroundColor="#43A047" VerticalOptions="Start" HeightRequest="50" Padding="20,0">  
  6.         <Image x:Name="imgAlert" Source="note" HeightRequest="24" WidthRequest="24" />  
  7.         <Label x:Name="LblMsg" TextColor="Black" FontSize="Micro" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" /> </StackLayout>  
  8. </pages:PopupPage>  

Now, we can wirte C# code and it gives a condition for error, warning, success, and note.

  1. using Rg.Plugins.Popup.Pages;  
  2. using Rg.Plugins.Popup.Services;  
  3. using System.Threading.Tasks;  
  4. using Xamarin.Forms;  
  5. using Xamarin.Forms.Xaml;  
  6. namespace XFPopupAnimation {  
  7.     [XamlCompilation(XamlCompilationOptions.Compile)]  
  8.     public partial class AlertPopup: PopupPage {  
  9.         public AlertPopup() {  
  10.             InitializeComponent();  
  11.         }  
  12.         public AlertPopup(string mtitle, string msg) {  
  13.             InitializeComponent();  
  14.             ChangecolorMsg(mtitle, msg);  
  15.         }  
  16.         private async void ChangecolorMsg(string mtitle, string msg) {  
  17.             if (mtitle == "W") {  
  18.                 imgAlert.Source = "warning";  
  19.                 Mainstk.BackgroundColor = Color.FromHex("#FCF8E3");  
  20.             } else if (mtitle == "S") {  
  21.                 imgAlert.Source = "success";  
  22.                 Mainstk.BackgroundColor = Color.FromHex("#43A047");  
  23.             } else if (mtitle == "E") {  
  24.                 imgAlert.Source = "error";  
  25.                 Mainstk.BackgroundColor = Color.FromHex("#F2DEDE");  
  26.             } else {  
  27.                 imgAlert.Source = "note";  
  28.                 Mainstk.BackgroundColor = Color.FromHex("#D9EDF7");  
  29.             }  
  30.             LblMsg.Text = msg;  
  31.             await Task.Delay(500);  
  32.             await Task.WhenAll(imgAlert.ScaleTo(1.3, 400), LblMsg.ScaleTo(1.1, 500), imgAlert.RotateTo(360, 600));  
  33.         }  
  34.         protected override void OnAppearing() {  
  35.             base.OnAppearing();  
  36.             HidePopup();  
  37.         }  
  38.         private async void HidePopup() {  
  39.             await Task.Delay(4000);  
  40.             await PopupNavigation.RemovePageAsync(this);  
  41.         }  
  42.     }  
  43. }  

Then, we go back to MainPage.xaml and create buttons for alerts.

  1. <Button x:Name="btnError" Text="Error!" Clicked="btnError_Clicked" />  
  2. <Button x:Name="btnWarn" Text="Warning!" Clicked="btnWarn_Clicked" />  
  3. <Button x:Name="btnSucc" Text="Success!" Clicked="btnSucc_Clicked" />  
  4. <Button x:Name="btnNote" Text="Note!" Clicked="btnNote_Clicked" />  

Here is the C# code.

  1. private async void btnError_Clicked(object sender, EventArgs e) {  
  2.     await Navigation.PushPopupAsync(new AlertPopup("E""Error!, Problem has been occurred while submitting your data."));  
  3. }  
  4. private async void btnWarn_Clicked(object sender, EventArgs e) {  
  5.     await Navigation.PushPopupAsync(new AlertPopup("W""Warning!, There was a problem with your Network Connection"));  
  6. }  
  7. private async void btnSucc_Clicked(object sender, EventArgs e) {  
  8.     await Navigation.PushPopupAsync(new AlertPopup("S""Success!, Your Message has been sent successfully."));  
  9. }  
  10. private async void btnNote_Clicked(object sender, EventArgs e) {  
  11.     //await Navigation.PushPopupAsync(new AlertPopup("N"));  
  12.     await Navigation.PushPopupAsync(new AlertPopup("N""Note!, Please read the comments carefully."));  
  13. }  
TA DA!!!!!!!!!!!!!!!

AlertPopup is working successfully.


Similar Articles