Sunday, October 22, 2017

Xamarin Forms - Passing parameters to android/ios application from another/url

This post includes a sample on how to use incoming parameters in Xamarin forms app.

How to pass parameters to android application (Forms app)

1. Create a Xamarin forms project.
2. In you Xamarin.Forms PCL project > App.xaml.cs create a new method that accepts either a class object or individual params (ConfigureParams()).
3. In this method, use the values to perform custom logic or navigate to page.
4. Go to platform specific project.
In MainActivity.cs file, use the following code to retrieve params from Intent Extras.
Example Code :

var page = Intent.GetStringExtra("page") ?? "MainPage";


5. Use app object to call (ConfigureParams()) and pass values retrieved via Intent.GetExtra methods.

How to pass paramters to ios application (Forms app)

Create a method in app.xaml.cs as mentioned above.
1. In platform specific ios project,  right click Components> Get Components and install Rivets component.
2. Override OpenUrl method in AppDelegate.cs
3. Save instance of App object created in FinishedLaunching() as a private variable (_app). In OpenUrl use the following code to retrive values from url.

Example code:

// url is NSUrl param which is passed to OpenUrl
var rurl = new Rivets.AppLinkUrl(url.ToString()):
var page ="";
if (rurl.InputQueryParameters.ContainsKey("page"))
       page= rurl.InputQueryParameters["page"];
_app.ConfigureParams(/* params*/);
return true;

4. Open info.plist and go to Advanced tab.  Configure a Url Type with required Identifier a UrlScheme. This will allow app to recognize different urls schemes.
5. You can test application by running in simulator and open the url with configured scheme in safari.

Saturday, October 21, 2017

XAF - Controller OnActivated not being called

Most common reasons for this to be happening:
1. Controller class is private.
2. Target view id is invalid.
3. TargetType is invalid.
4. Inheriting from incorrect version of ViewController base class. There are multiple versions using generics etc.(!)