+ All Categories
Home > Technology > Chuck Norris Xamarin

Chuck Norris Xamarin

Date post: 23-Jan-2018
Category:
Upload: james-quick
View: 292 times
Download: 0 times
Share this document with a friend
46
Click “New Solution”
Transcript
Page 1: Chuck Norris Xamarin

Click “New Solution”

Page 2: Chuck Norris Xamarin

Choose Xamarin.Forms App and click Next

Page 3: Chuck Norris Xamarin

Name Your App, Check Shared Library, then click Next

Page 4: Chuck Norris Xamarin

Name Your Project, Choose a location, and click Create

Page 5: Chuck Norris Xamarin

This is what your solution should look like. A Shared Folder and one for Droid and IOS

Page 6: Chuck Norris Xamarin

Right Click on the Shared Project and choose Add->New File

Page 7: Chuck Norris Xamarin

Choose Forms ContentPage and call it Home.cs, then click New

Page 8: Chuck Norris Xamarin

This is what Your New Home page should look like.

Page 9: Chuck Norris Xamarin

Open the <NameofYourProject>.cs file and remove the highlighted code

Page 10: Chuck Norris Xamarin

Replace it with this

Page 11: Chuck Norris Xamarin

• public App (){// The root page of your applicationMainPage = new Home();

}

Page 12: Chuck Norris Xamarin

Navigate back to the Home.cs file and remove the code shown here.

Page 13: Chuck Norris Xamarin

Replace it with this which creates a StackLayout to hold our content and adds padding for IOS

Page 14: Chuck Norris Xamarin

StackLayout stack = new StackLayout {Children = {}

};this.Content = stack;this.Padding = new Thickness (0, Device.OnPlatform (20, 0

, 0), 0, 0);

Page 15: Chuck Norris Xamarin

Add a Title Label as shown above, then include it in the Children of the StackLayout.

Page 16: Chuck Norris Xamarin

var title = new Label {Text = "Chuck Norris App",HorizontalOptions = LayoutOptions.Center,VerticalOptions = LayoutOptions.StartAndExpand

};

StackLayout stack = new StackLayout {Children = {title}

};

Page 17: Chuck Norris Xamarin

Create two more Labels, subtitle and jokeText, and add them to the Children of the StackLayout

Page 18: Chuck Norris Xamarin

var title = new Label {Text = "Chuck Norris App",HorizontalOptions = LayoutOptions.Center,VerticalOptions = LayoutOptions.StartAndExpand

};var subtitle = new Label {

Text = "Click below to get a hilarious joke",HorizontalOptions = LayoutOptions.Center,VerticalOptions = LayoutOptions.StartAndExpand

StackLayout stack = new StackLayout {Children = {title, subtitle, jokeText}

};

Page 19: Chuck Norris Xamarin

Should end up like this

Page 20: Chuck Norris Xamarin

Click the Run Button (looks like a play button) to open the emulator and start your app

Page 21: Chuck Norris Xamarin

Your app should look like this for now

Page 22: Chuck Norris Xamarin

Now let’s add a jokeButton and it’s “Click Handler” The click handler is where we tell the app what to do when the buton

is clicked.

Page 23: Chuck Norris Xamarin

Add a new file to our Shared Project.

Page 24: Chuck Norris Xamarin

Button jokeButton = new Button {Text = "Get Joke",HorizontalOptions = LayoutOptions.Center,VerticalOptions = LayoutOptions.StartAndExpand

};//This is what will happen when the jokeButton is clickedjokeButton.Clicked+= async (sender, e) => {

};

Page 25: Chuck Norris Xamarin

Click “New Solution”

Page 26: Chuck Norris Xamarin

Replace the code in that file with the code above.

Page 27: Chuck Norris Xamarin

namespace ChuckNorris{

public class Joke{

public string type { get; set; }public Value value { get; set; }

}

public class Value{

public int id { get; set; }public string joke { get; set; }public List<object> categories { get; set; }

}}

Page 28: Chuck Norris Xamarin

Right Click on “List” and choose Resolve-> using System.Collections.Generic

Page 29: Chuck Norris Xamarin

Now add the above lines in the Click Handler for the jokeButon which will attempt to retrieve a joke from the

Web

Page 30: Chuck Norris Xamarin

jokeButton.Clicked+= async (sender, e) => {//Attempt to retrieve joke from the webHttpClient client = new HttpClient();Uri uri = new Uri("http://api.icndb.com/jokes/random?li

mitTo=[nerdy]&firstName="+ firstNameEntry.Text+"&lastName="+ lastNameEntry.

Text);string obstring = await client.GetStringAsync (uri);

};

Page 31: Chuck Norris Xamarin

Right Click on “HttpClient” and choose Resolve->using System.Net.Http

Page 32: Chuck Norris Xamarin

Right Click on Package in the Android Project and Choose Add Packages

Page 33: Chuck Norris Xamarin

Search Json.net and click Add Package

Page 34: Chuck Norris Xamarin

Right Click on Package in the IOS Project and Choose Add Packages

Page 35: Chuck Norris Xamarin

Search Json.net and click Add Package

Page 36: Chuck Norris Xamarin

Joke joke = JsonConvert.DeserializeObject<Joke> (obstring);jokeText.Text = joke.value.joke;

Page 37: Chuck Norris Xamarin

Right Click JsonConvert and choose Resolve->using Newtonsoft.Json

Page 38: Chuck Norris Xamarin

Run the app again and click the Get Joke button

Page 39: Chuck Norris Xamarin

You should see a joke appear

Page 40: Chuck Norris Xamarin

Add two Entries, firstNameEntry and lastNameEntry as above

Page 41: Chuck Norris Xamarin

Entry firstNameEntry = new Entry {Text = "First Name",HorizontalOptions = LayoutOptions.CenterAndExpand,VerticalOptions = LayoutOptions.StartAndExpand,

};

Entry lastNameEntry = new Entry {Text = "Last Name",HorizontalOptions = LayoutOptions.CenterAndExpand,VerticalOptions = LayoutOptions.StartAndExpand,

};

Page 42: Chuck Norris Xamarin

Add them to the Children of the StackLayout

Page 43: Chuck Norris Xamarin

Update the uri variable to include the two entry fields as above

Page 44: Chuck Norris Xamarin

Uri uri = new Uri("http://api.icndb.com/jokes/random?limitTo=[nerdy]&firstName="

+ firstNameEntry.Text+"&lastName="+ lastNameEntry.Text);

Page 45: Chuck Norris Xamarin

Rerun the solution. Enter your name into the two entries

Page 46: Chuck Norris Xamarin

You should get back a personalized joke as Above


Recommended