Xamarin.Auth Custom Accounts
Xamarin.Auth Custom Account
Hey there today i will explain how do you create a custom account with Xamarin.Auth component who allows create accounts with Facebook, Twitter and OAuth logins, but sometimes we need some more for example extra information to add to account store.
Lets Start
First we need create a new Xamarin.Android Project
Then we continue to downloading Xamarin.Auth Component don't be afraid ITS FREE =)
We need to create two layouts:
Main.axml
Second.axml
Now we can code =D
The first class that we code its a class who contains a static method to return IDictionary with the properties of our custom account:
public class AccountCore { public static IDictionary<string, string> Dictionary(string FName, string LName, string Age, string Email) { IDictionary<string, string> d = new Dictionary<string, string> (); d.Add ("FName",FName); d.Add ("LName", LName); d.Add ("Age",Age); d.Add ("Email",Email); return d; } }We continue with the MainActivity.cs
firts we need to add an using
using Xamarin.Auth;
MainActivity's Code
[Activity (Label = "XamarinA", MainLauncher = true)] public class MainActivity : Activity { //Button to save data Button bSave; //Textfield to write data EditText eFirstName, eLastName, eAge, eEmail; protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); // Set our view from the "main" layout resource SetContentView (Resource.Layout.Main); //button and textfields assignations bSave = FindViewById<Button> (Resource.Id.Btnsave); eFirstName = FindViewById<EditText> (Resource.Id.FNametxt); eLastName = FindViewById<EditText> (Resource.Id.LNametxt); eAge = FindViewById<EditText> (Resource.Id.Agetxt); eEmail = FindViewById<EditText> (Resource.Id.EMailtxt); //set button click event bSave.Click += bSave_HandleClick; //check if account already exists var accounts = AccountStore.Create (this).FindAccountsForService ("XASample"); foreach (var a in accounts) { //if the account has stored we proceed to start next activity StartActivity (typeof(SecondActivity)); break; } } //Button Click event void bSave_HandleClick (object sender, EventArgs e) { string FName = eFirstName.Text; string LName = eLastName.Text; string Age = eAge.Text; string Email = eEmail.Text; //Create Account variable with a new Account(username, Dictionary of properties) Account ac = new Account(FName, AccountCore.Dictionary(FName,LName,Age,Email)); //Save the account we create with(Account,ServiceId) //for service id you can use an unique id of your app AccountStore.Create (this).Save (ac, "XASample"); StartActivity (typeof(SecondActivity)); } }
Second Activity Code
[Activity (Label = "SecondActivity")] public class SecondActivity : Activity { //Button to delete data Button bDelete; //Labels to view data TextView tFirstName, tLastName, tAge, tEmail; protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); SetContentView (Resource.Layout.Second); //button and labels assignations bDelete = FindViewById<Button> (Resource.Id.Btndelete); tFirstName = FindViewById<TextView> (Resource.Id.FNamelbl); tLastName = FindViewById<TextView> (Resource.Id.LNamelbl); tAge = FindViewById<TextView> (Resource.Id.Agelbl); tEmail = FindViewById<TextView> (Resource.Id.EMaillbl); bDelete.Click += bDelete_HandleClick; //check if account already exists var accounts = AccountStore.Create (this).FindAccountsForService ("XASample"); foreach (var a in accounts) { //here you can use the account properties to set label's text tFirstName.Text = a.Properties["FName"]; tLastName.Text = a.Properties["LName"]; tAge.Text = a.Properties["Age"]; tEmail.Text = a.Properties["Email"]; } } //button click event void bDelete_HandleClick (object sender, EventArgs e) { //first search for accounts with the id and then we continue to delete it var accounts = AccountStore.Create (this).FindAccountsForService ("XASample"); foreach (var a in accounts) { //delete each of accounts found AccountStore.Create (this).Delete (a, "XASample"); } //returns to the firts activity StartActivity (typeof(MainActivity)); } }
Finally, now you have a custom Xamarin.Auth account let, see the results
If you have a question feel free to ask me.
You can found a complete example in my github:
https://github.com/AlejandroRuiz/Mono/tree/master/XamarinA
Some music to relax =)
Este comentario ha sido eliminado por el autor.
ResponderEliminar