Skip to content

Getting started

Tyson Elliot Hooker edited this page Jan 12, 2021 · 9 revisions

Getting Started with Rg.Plugins.Popup

One version of the plugin must be installed to each native project and PCL/Net.Standard before to be used.

Initialization

The plugin requires to be initialized. To use a PopupPage inside an application, each platform application must initialize the Rg.Plugins.Popup. This initialization step varies from platform to platform and is discussed in the following sections.

iOS

[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
      Rg.Plugins.Popup.Popup.Init();
      
      global::Xamarin.Forms.Forms.Init ();
      LoadApplication (new App ());
      return base.FinishedLaunching (app, options);
    }
}

Android

namespace HelloXamarinFormsWorld.Android
{
    [Activity(Label = "HelloXamarinFormsWorld", MainLauncher = true,
        ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            Rg.Plugins.Popup.Popup.Init(this);
        
            Xamarin.Forms.Forms.Init(this, bundle);
            LoadApplication (new App ());
        }
    }
}

Also, you need to override OnBackPressed for MainActivity. For more information see Android Back Button chapter

Universal Windows Platform

In Universal Windows Platform (UWP) applications, the Init method that initializes the Rg.Plugins.Popup is invoked from the App class:

Rg.Plugins.Popup.Popup.Init();
Xamarin.Forms.Forms.Init(e, Rg.Plugins.Popup.Popup.GetExtraAssemblies());

if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
  ...
}

You can read more information about "why do I need to use GetExtraAssemblies" in the troubleshooting documentation page.

Mac

 [Register("AppDelegate")]
    public class AppDelegate : FormsApplicationDelegate
    {
        public AppDelegate() ...

        public sealed override NSWindow MainWindow { get; }

        public override void DidFinishLaunching(NSNotification notification)
        {
            Rg.Plugins.Popup.Popup.Init();
            Forms.Init();
            LoadApplication(new App());
            base.DidFinishLaunching(notification);
        }
    }

Tizen

	class Program : global::Xamarin.Forms.Platform.Tizen.FormsApplication
	{
               	protected override void OnCreate() ...
		static void Main(string[] args)
		{
			var app = new Program();
                        Rg.Plugins.Popup.Tizen.Popup.Init();
			Forms.Init(app, true);
			app.Run(args);
		}
	}

Android Back Button

For Android back button to work with the plugin, you should invoke Rg.Plugins.Popup.Popup.SendBackPressed in your MainActivity in override method OnBackPressed

For the Base Case usage, you can simply use

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    ...

    public override void OnBackPressed()
    {
        Rg.Plugins.Popup.Popup.SendBackPressed(base.OnBackPressed);
    }
    ...
}

However, since Rg.Plugins.Popup.Popup.SendBackPressed(base.OnBackPressed) returns a bool, you can use this to create special behaviours for the back button press that are dependant on whether or not there are any pages currently on the popup stack

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    ...

    public override void OnBackPressed()
    {
        if (Rg.Plugins.Popup.Popup.SendBackPressed(base.OnBackPressed))
        {
            // Do something if there are some pages in the `PopupStack`
        }
        else
        {
            // Do something if there are not any pages in the `PopupStack`
        }
    }

    ...
}