user1407804
user1407804

Reputation:

Xamarin: Simple Cross platform Alarm app

Goal: Developing a cross platform mobile app using Xamarin studio that shows a message at certain date and time.

Questions:

a) What things will/can be shared between platforms if i want to develop such application. (A structural design With possible classes will be appreciated)

b) Is it better to use a Timer or AlarmManager?

c) What type of project should be created for the Shared Core Project (Portable Class Library/Platform Specific Library/Plain C# Project)?

Thanks in Advance.

Upvotes: 11

Views: 9641

Answers (4)

Magnus Ahlin
Magnus Ahlin

Reputation: 655

I know this is an old post but anyone wanting to do this nowadays should definitely look at Xamarin.Forms that came out earlier this year which is a set of truly cross platform controls so you only create one UI

Upvotes: 2

Gaurav Deochakke
Gaurav Deochakke

Reputation: 2283

Xamarin, as of now, can help you in sharing the business logic. i.e. and database, web service or any such code which will have no dependency upon what platform you are working upon. You code in c#. So you have an access to the basic c# features, or to be verbally correct, the mono-specific features that you can make use of.

As a conclusion, what i can summarize is: you can create a separate project for the back end of your work, and a separate project for the UI and its code behind. (both obviously being in the same work space). Then you can use this central Core kind of back end project in an IOS app, android app and by default in a WP8 app (even a mac and windows app). So except the Core, you need to put some extra efforts in coding separately for all platforms with respect to the UI, its handlers and the code behinds.

Upvotes: 2

Ben Bishop
Ben Bishop

Reputation: 1414

I've been focusing on how to best share code between Android and iOS these past couple of weeks and here are my suggestions based on my experiences:

a) In general, you can share anything between platforms that is not platform specific.

I tend to use presenters or mediators that hold a reference to a view that is defined by an interface. For example, if I had a Login View, I would have my iOS UIViewController and my Android Activity both implement an ILoginView interface. This interface would have the following defined:

  • Getters for Username and Password
  • An event called LoginSubmitted
  • Two methods ShowLoginFailed and GoNextScreen

The presenter/mediator would be responsible for listening to the login being submitted, get the username/password, and passing those things off to a service call or command. Whenever the mediator received the results of the command/service it would then be responsible for invoking either ShowLoginFailed or GoNextScreen.

I have also used interfaces for defining my SQLite Manager (https://github.com/praeclarum/sqlite-net/tree/master/src) , or a delegate that is responsible for making the actual web API service call. I've found I've had to do this because there is some platform specific compiling that needs to happen for the SQLite manager and RestSharp.

Long story short, interfaces are your friend especially when you couple them with some sort of Dependency Injection system.

b) If you are wanting your user to be prompted with an alarm when their device is in sleep mode or when they are in another app, I'd use AlarmManager for Android and LocalNotification for iOS.

c) You can kind of sort of use PCLs with Xamarin.Android and Xamarin.iOS. The problem is that PCLs are not 100% supported yet. However, there are some tweaks you can make to your dev environment that should hopefully get you where you want:

  • Switch to the Beta or Alpha channel in Xamarin Studio: "Xamarin Studio" -> "Check for Updates" -> Change the channel in the combo box in the upper left of the dialog box that pops up -> Select "Restart and Install Updates." This will let you have both Android and iOS projects be able to link to the same PCL in the same solution.

  • After updating XS, close XS and follow the instructions in this post to modify your Mono install so PCLs properly compile: MonoDevelop: is it possible to switch PCL's compiler?

  • To get code hinting to play nicer inside your PCL project, you'll need to grab the mscorlib.dll from your Mono install and place it in a DLLs folder in your project directory and add a reference to it in your PCL project. You can find the dll here: /Library/Frameworks/Mono.framework/Versions/3.0.10/lib/mono/4.0

Once you have done all of that, you should be in pretty good shape getting your PCL to work. I've found that this has been worth the extra work in that I can now create a Unit Test project in XS link it to the PCL and run tests from within the IDE. In addition, I can use libs like Moq to mock my views and do integration tests.

Xamarin is promising that full PCL support will be coming "soon" and we won't have to bother with these hacks.

Upvotes: 18

Martin
Martin

Reputation: 1914

Check out this guide from the Xamarin team to get started: http://docs.xamarin.com/guides/cross-platform/application_fundamentals/building_cross_platform_applications

This is the 4th part that addresses your question in part C. http://docs.xamarin.com/guides/cross-platform/application_fundamentals/building_cross_platform_applications/part_4_-_platform_divergence_abstraction_divergent_implementation

In addition, the Xamarin Studio Dashboard/Main Screen(?) on the right side has 2 applications you can download that fully work and target 2-3 platforms so you can see how they structure the application when sharing code between projects.

There are a lot more guides and tutorials on their site that don't take much time to read through and practice with.

Upvotes: 1

Related Questions