This course will be retired on July 14, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Introduction to Xamarin!
You have completed Introduction to Xamarin!
Preview
Now that we've seen our options for sharing code across platforms, let's use the shared library in our project to eliminate code duplication.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
So, we have walked through both Android
and iOS, but we have not shared any code.
0:00
We wrote the same code in both platforms
to calculate the number of pizzas.
0:07
So let's start by creating
a service to do the calculations.
0:12
In the shared project,
create a pizza calculation service, and
0:16
add a Calculate method.
0:20
The Calculate method uses a string
as a parameter and returns a string.
0:41
So we can move the int parsing calculation
and response into this method.
0:48
We could also modify it to return
a more informative response.
0:53
Now we'll go back to the Android project.
1:43
In the main activity,
new up a pizzaCalculatorService, and
1:48
add the calculate to
the click event delegate.
1:52
In the iOS project, the ViewController
has a similar event handler for
2:58
TouchUpInside, which can also be changed
to use the pizzaCalculatorService.
3:02
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up