In this series we will examine highlights of Xamarin platform. Enjoy!
UX is usually done via wireframes or mockups using tools such as Balsamiq, Mockingbird, Visio, or just plain ol’ pen and paper. UX Mockups allow you to quickly design UX without having to worry about the actual UI design.
Shared Projects let you write common code that is referenced by a number of different application projects. The code is compiled as part of each referencing project and can include compiler directives to help incorporate platform-specific functionality into the shared code base.
Shared projects structure
The simplest approach to sharing code files is to use a Shared Project (introduced in Xamarin Studio 5 and Visual Studio 2013 Update 2).
Unlike most other project types a shared project does not have any output (in DLL form), instead the code is compiled into each project that references it. This is illustrated in the diagram below – conceptually the entire contents of the Shared Project is “copied into” each referencing project and compiled as though it was a part of them.
Each platform allows user interface controls to be added to a screen using code.
This can be very time-consuming as it can be difficult to visualize the finished design when hard-coding pixel coordinates for control positions and sizes.
Programmatically creating controls does have benefits though, particularly on iOS for building views that resize or render differently across the iPhone and iPad screen sizes.
There are some situations where your shared code will still need to work differently on each platform, possibly accessing classes or features that behave differently. Conditional compilation works best with Shared Asset Projects, where the same source file is being referenced in multiple projects that have different symbols defined.
A trivial example that writes and reads a text file is shown below. Using Environment.GetFolderPath allows the same code to run on iOS and Android, which each return a valid directory based on their filesystem conventions.
The Isolated Storage APIs are not available in Portable Class Libraries. One alternative for PCL is the PCLStorage NuGet.
A simple parallel task operation might look like this:
To make UI thread calls
You should not use the same SQLite database connection across multiple threads.
Be careful to open, use and then close any connections you create on the same thread.
The code is in a single file
sample sqlite queries
Selecting an object using SQL
A Shared Project does not get compiled when it has nothing referencing it, so syntax (or any other) errors will not be highlighted until it has been referenced by something else.
To configure the components directory, a components.config file will need to be created if it doesn’t already exist. This file should contain the following information:
IDisposable is not a destructor, and should only be implemented in the following circumstances
- When the class owns unmanaged resources.
- Typical unmanaged resources that require releasing include files, streams, and network connections.
- When the class owns managed IDisposable resources.
A weak reference is created with the WeakReference type, as shown in the following code example:
Weak references are useful for objects that use a lot of memory, but which can be recreated easily if they are reclaimed by garbage collection.
As a general rule, if you are building fixed layouts (especially side-by-side elements) allow at least 50% more width than your English strings require for labels and text. This won’t solve every problem but will provide a buffer that will work in many cases.
Displaying HTML in a WebView control using Xamarin.Android is accomplished in just a few lines of code:
Razor .cshtml templates can also be included in the PCL so they’re easily shared across platforms.