Loading...
Menu

Top 10 Xamarin Tips and Traps

Thanks for checking out [_Top 10 Xamarin Tips and Traps. _]Whether you are an experienced or new mobile app developer, the ten topics presented in this eBook provide insights into Xamarin’s capabilities and resources to help you get started with it. Among the topics you’ll learn about:

  • accessing Xamarin and GitHub resources
  • versioning with Xamarin
  • integrating NuGet packages
  • increasing app performance
  • rendering data natively

If you’re considering writing apps in Xamarin, you probably already know that it enables you to create cross-platform native apps that run on Apple iOS, Android, and Universal Windows Platform (UWP). You also probably know that the Xamarin SDK and its .NET core are open source development platforms for iOS, Android, Universal Windows Platform (UWP), and Mac.

You might also think you know about Xamarin’s design-time trade-offs for its cross-platform code sharing. Why the italics? Because a new tool from Infragistics—Ultimate UI for Xamarin—eliminates those trade-offs and makes Xamarin app development a no-compromise solution.

Meet—and Try—Ultimate UI for Xamarin

Xamarin is a versatile development framework, and this book will give you valuable tips on how to use it. We think you’ll go even farther, faster, if you also check out Infragistics Ultimate UI for Xamarin before you start coding. Ultimate UI for Xamarin combines lightning-fast controls with a RAD WYSIWYG design-time experience, empowering you to build beautiful, quality, high-performing applications with a rich UX and a robust feature set.

Ultimate UI for Xamarin includes flexible iOS, Android, and Xamarin.Forms controls designed for the most demanding apps, outperforming all other Xamarin controls on the market. Ultimate UI for Xamarin also includes the ground-breaking Xamarin.Forms Productivity Pack, delivering unparalleled time-savings and productivity for Xamarin.Forms developers. The Productivity Pack includes “AppMap,” a visual page-creation tool call, and control configurators which eliminate the need for manual XAML coding, greatly speeding up app control layout. You’ll also get page templates and code snippets.

  • It’s Fast: Even with real-time and large data sets, no other Xamarin grids and data chart controls outperform Infragistics Ultimate UI for Xamarin.
  • It’s Versatile: Ultimate UI for Xamarin includes iOS and Android native controls, along with Xamarin.Forms controls for maximum code-sharing.
  • It’s Easy to Use: AppMap, control configurators, and templates enable rapid application view and view model creation and visual control configuration.
  • It’s Easy to Learn: Visual control configurators enable you to easily and rapidly learn rich Xamarin Forms controls.

In short, Ultimate UI for Xamarin is the first no-compromise library of controls and productivity tools for Xamarin. And you can try it right now for free.

Learn More

Once you install Ultimate UI for Xamarin, get productive in just a few minutes with six write fast, run fast lessons for you to watch, read, and try.

When you’re ready to dig in a little more deeply, check out Moo2U—an end-to-end, best practices Xamarin.Forms reference application, built with Ultimate UI for Xamarin’s Productivity Pack and UI widgets. Moo2U not only demonstrates how to build a stylish, scalable Xamarin.Forms app.

We look forward to hearing how you like this book…and how we expect and hope you’ll love Ultimate UI for Xamarin.

Best,

Ken Rosen ([email protected])
Sr. Director, Product Management
Infragistics

  1. TIP—Accessing Xamarin resources
  2. TIP—Using GitHub resources
  3. TRAP—Deploying and testing with emulators
  4. TIP—Using NuGet
  5. TIP—Implementing grid layouts with Xamarin
  6. TIP—Optimizing data rendering across platforms
  7. TIP—Boosting cross-platform performance in Xamarin
  8. TRAP—Using control and data templates
  9. TIP—Using .NET Core
  10. TIP—Releasing an Android app
  11. TIP—Using the Prism framework

Try Ultimate UI for Xamarin for Free: bit.ly/UltimateXamarin

Accessing Xamarin resources

Learning how to use Xamarin and its components to develop native applications is like learning how to ride a bicycle: It might seem difficult or complicated at first, but becomes simple with practice. As with anything that you learn to do the first time, preparation is a crucial element of success. There are common pitfalls, mistakes, or oversights that can occur for even the most experienced developer. Fortunately, there are tons (literally, if you printed them!) of resources available to help ensure your success. These include resources from the following providers: Xamarin SDKs

  • Xamarin University
  • Xamarin Developer Center
  • Xamarin on edX
  • Microsoft Virtual Academy
  • Third-party resources

Xamarin SDKs

The source code for Xamarin.iOS, Xamarin.Mac, Xamarin.Android, and Xamarin.Forms is available from the Xamarin website at http://open.xamarin.com as well as on the Xamarin GitHub. The open source nature of Xamarin enables you to create native apps for any device in C# or F#. You can also take advantage of open source bindings and native libraries for Facebook and Google Play, as well as useful functions such as messaging and GPS.

The source code, bindings, and libraries are available through the Xamarin GitHub. (For more information, see TIP: Using GitHub resources.)

Xamarin University

Xamarin University is an online platform that provides live and interactive training, including five free, on-demand classes in the Visual Studio Dev Essentials program. Xamarin University also offers self-guided classes, such as those shown in Figure 1, that you can work through at your own pace to learn Xamarin.

  1. The first few classes in the Xamarin University portal.
    p<. Xamarin University also offers a Xamarin certification. You can combine the instruction from the self-guided classes, videos, and other available resources to complete the certification. You can access Xamarin University at https://university.xamarin.com.

Xamarin Developer Center

The Xamarin Developer Center provides a collection of online resources that help you successfully develop mobile apps. The Developer Center includes Xamarin documentation, guides, workbooks, recipes, samples, and more for the various platforms. Dozens of guides are available for cross-platform development and for individual platforms. Figure 2 shows the current categories of Xamarin Developer Center content.

  1. Xamarin Developer Center category links.
    p<. Some of the resources available from the Developer Center include:

  • Interactive workbooks that enable you to experiment and explore within real projects. You can edit code and text to customize the project and practice using different components of Xamarin. Workbooks are available for Azure, Mac, mobile and other platforms.
  • Recipes that document a specific Xamarin component or resource. Recipes are available for the GPS, accelerometer, geocoder, and other device resources.
  • APIs and sample projects that use common Xamarin features. Samples of 3-D scenes, physics manipulation, animation, rendering, lighting, and other features are available for various platforms.
  • Add-in components for Xamarin and Visual Studio that simplify or add configuration utilities to the development process. Components are available for cloud services, plug-ins, themes, development resources, and more.
    p<. You can review the available documentation and resources in the Xamarin Developer Center at http://developer.xamarin.com.

Xamarin on edX

Microsoft also offers courses on the edX online training platform to provide self-paced developer-related courses for subjects including Microsoft Azure, Microsoft Exchange Server, and Xamarin. Currently available courses include a two-week introduction course to Xamarin.Forms, and a six-week Programming with C# course that includes useful information for new application developers. To locate the Xamarin.Forms course, search for Xamarin on http://www.edx.org.

Microsoft Virtual Academy

The Microsoft Virtual Academy (MVA), provides free video-based training for a variety of Microsoft and fundamental technologies. Several beginning, intermediate, and advanced Xamarin courses are available, including Xamarin for Absolute Beginners, designed for experienced .NET developers who want to use Xamarin. You can find the MVA at http://mva.microsoft.com.

Third-party resources

Many websites, including those described here, provide learning materials related to Xamarin.

  • Pluralsight is a subscription-based learning portal that offers courses for a wide range of technologies. As of this writing, Pluralsight offers over 125 Xamarin-related courses, ranging in length from 45 minutes to eight hours. Pluralsight is located at http://www.pluralsight.com.
  • Lynda.com offers subscription-based learning that includes Xamarin-related resources. You can learn more at http://www.lynda.com.
  • Tuts+ is a free online resource that provides how-to tutorials for various technologies. Several Xamarin-related tutorials are available. You can learn more at http://www.tutsplus.com.

Try Ultimate UI for Xamarin for Free: bit.ly/UltimateXamarin

Using GitHub resources

Xamarin and Infragistics provide a vast amount of resources through their respective GitHub sites. Many open license projects are available on GitHub and can be used immediately in your next app. If the foundation for a module is available on GitHub, it isn’t necessary for you to create it from scratch.

Xamarin GitHub repositories

The Xamarin GitHub has dozens of repositories for samples, components, workbooks, and more. The most popular Xamarin repositories include:

  • Xamarin.Forms
  • Xamarin.MaciOS
  • Xamarin.Android

Each of these repositories has several available branches and builds. Each build includes a readme file that contains the build requirements and configuration steps for using the build. For example, the build requirements for a recent version of Xamarin.MaciOS include:

  • Autoconf, automake, and libtool
  • CMake
  • Xcode
  • Mono SDK
  • Xamarin Studio

The readme file describes how to install the dependencies that are required to use the module. The primary Xamarin repositories can be found at https://github.com/xamarin.

Reporting a bug

Xamarin handles bug reporting through an instance of Bugzilla that is available at https://bugzilla.xamarin.com/newbug. If you encounter a problem with any of the modules in the Xamarin repositories, provide the following information in the bug report:

  • The steps to reproduce the bug.
  • The expected behavior or result.
  • The actual behavior or result.
  • Any available supporting information, such as log files, images, or videos.
  • The development or testing environment in which the bug occurs.

Samples

Another GitHub repository and resource for developing apps is Mono for Android and MonoTouch for iOS. Both iterations of Mono provide sample projects and use the native API for each platform to provide rich features and capabilities for the given platform. A typical sample project is the tip calculator shown in Figure 3, which was recently updated for Xamarin.Forms. This sample is available at https://developer.xamarin.com/samples/xamarin-forms/TipCalc.

  1. Tip calculator sample.
    p<. As previously mentioned, the Xamarin Developer Center provides samples for multiple platforms. Samples can be organized by platform, including:

  • Cloud
  • Data
  • Games
  • Operating system version
  • Navigation
  • Notifications
  • Physics
    p<. You can download a compressed folder of these samples that contain the solution files, or you can browse the code for each sample on GitHub.

Infragistics GitHub repositories

Infragistics also maintains GitHub repositories for Ignite UI, the ReportPlus component, and other components. The Infragistics repositories are available from https://github.com/Infragistics. The IgniteUI repositories are available from https://github.com/IgniteUI.

Mono

Mono is the underlying foundation that enables cross-platform development using Xamarin. Mono uses an open source version of .NET and is licensed under the MIT license. The Xamarin SDKs for iOS and Android and the source code for these projects have been contributed to the .NET Foundation.

Docker

Docker enables the use of containers and images to run applications in a continuous integration/continuous deployment model. Docker is open source, and is available for the Windows Server 2016, Linux, and Mac OS X platforms. You can configure applications to run on specific versions of an operating system that is used as the container. For example, you can combine Mono and Docker to create containerized versions of applications that run without any additional operating system configuration.

Try Ultimate UI for Xamarin for Free: bit.ly/UltimateXamarin

Deploying and testing with emulators

Chances are that you don’t have dozens of mobile devices running all the versions of the mobile operating systems you want to deploy and test your mobile app on. You can avoid the expense and trouble of maintaining a full array of test devices by using virtual emulators such as the one shown in Figure 4. However, this does introduce another layer of complexity.

  1. An Android emulator on Hyper-V.
    p<. Visual Studio uses the Client Hyper-V functionality that was introduced with Windows 8. This is the same enterprise virtualization technology capability that is available in the Windows Server OS, but at the client level. Instead of needing dozens of devices that run different OS versions, you can use virtual machines (VMs) that emulate each version of the software. Visual Studio will communicate with the VMs and automatically deploy your app on the VM when testing. The Android emulators through Visual Studio are typically must faster than the emulators provided by Google.

If you are running Visual Studio in a VM already, then you must configure additional virtualization extensions to run Hyper-V nested within a VM. Nested virtualization is only supported on Windows Server 2016 and Windows 10 Anniversary Update. For more information, visit https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/user-guide/nested-virtualization.

Four Hyper-V components that you need to be aware of when testing mobile apps on VMs are:

  • Dynamic memory
  • File storage locations
  • Virtual switches
  • Code change deployment

Dynamic memory

To effectively test your apps on VMs, your development machine must have the same storage and memory specifications that the mobile device will have. Most mobile devices have only a small amount of RAM and storage, so one mobile OS or app doesn’t require significant resources. However, if you plan to test several devices or OS versions, you might need more RAM than is available off the shelf. Figure 5 shows the memory settings of the emulator VM.

  1. Memory settings of the Windows Phone OS emulator.
    p<. Hyper-V VMs for all operating systems have dynamic memory that expands when the VM requests it, from a minimum amount to a maximum that you specify. In the example shown in Figure 6, the emulator will start with 512 MB of RAM. If you are testing only the functionality of your app, you can enable dynamic memory to provide the emulator with as much RAM as it requests. However, this can provide you with some false hope as you test your application, because you might not be aware of it when the emulator exceeds the RAM that would typically be found on a mobile device. You should disable dynamic memory, and set a hard limit on the maximum amount of RAM the emulator can use to more accurately test how an actual mobile device will respond.

File storage locations

All Hyper-V VM images and files are stored in the user directory, which typically resides on the local OS drive. Therefore, if you are running Visual Studio on a laptop or other computer that has only one drive, you might see slower storage performance than the actual mobile device would have. Figure 6 shows the storage settings for an Android-based emulator running KitKat.

  1. An Android emulator stored on the OS drive.
    p<. To more accurately emulate the dedicated storage space of a mobile device, run your emulators on a separate data drive.

Virtual network switches

Hyper-V uses virtual switches to bridge the network connection of a VM or emulator with the actual network adapter, whether physical or wireless, of the host computer. Figure 7 shows the Hyper-V Virtual Switch Manager for a host computer that has five switches configured.

  1. The Virtual Switch Manager for Hyper-V.
    p<. Hyper-V uses three virtual switch connection types :

  • External connections create a bridge from the VM to the adapter on the host.
  • Internal connections provide a separate network, similar to a virtual LAN (VLAN), that enables communication between the host and VMs but does not provide Internet access.
    p<. Private connections provide a segmented network for internal VM ca communications without access to the host or the Internet.

In most cases, you would want to use an external switch for your emulators or VMs so that they have the same network access as the host computer. If you have multiple network adapters, you can assign a dedicated connection for the VMs from the drop-down list. If you have only one network adapter, the Allow management operating system to share this network adapter check box must remain selected to allow the host OS to share the adapter. Otherwise, Hyper-V will completely control the adapter and the host OS will lose network connectivity.

Code change deployment

When using emulators to test an app, Xamarin and Visual Studio can sometimes try to be overly helpful. Small changes to your app code might not always be deployed to the emulator. If you think that code changes haven’t been deployed, delete the app from within the emulator, clean the solution in Visual Studio, and then retry the deployment. Cleaning the solution should include deleting the bin and object folders. This will ensure that the latest build is pushed to the emulator. It is also important to note that emulators can perform very differently from actual hardware devices. Before you release an app, you should test the app on as many physical devices that you realistically can.

You can find a walkthrough of setting up Visual Studio for use with Xamarin at https://msdn.microsoft.com/en-us/library/dn879698.aspx, and the steps for verifying that your environment is ready for mobile app development at https://msdn.microsoft.com/en-us/library/mt488769.aspx.

Try Ultimate UI for Xamarin for Free: bit.ly/UltimateXamarin

Using NuGet

If you have any previous experience with .NET, you are probably familiar with using NuGet. NuGet is the package manager for developing .NET applications. Packages can be integrated into an app by using NuGet. Xamarin offers several packages that can be combined with existing .NET packages to enable cross-platform development. To navigate to the NuGet package manager in Visual Studio, right-click a solution, and then click Manage NuGet Packages for Solution, as shown in Figure 8.

  1. Managing NuGet packages from within Visual Studio.
    p<. Adding a package to a solution places it in the References tree of the solution. After a package is added to a solution, you can use the APIs while developing your app. Even a simple cross-platform application can have several packages integrated with it. For example, the solution for the WeatherApp tutorial on MSDN includes over a dozen NuGet packages. (The WeatherApp tutorial is available at https://msdn.microsoft.com/en-us/library/dn879698.aspx.)

The NuGet package manager, shown in Figure 9, makes it easy to install packages from within Visual Studio and update the packages you have integrated.

  1. NuGet packages integrated with a sample app.
    p<. The Updates tab of the package manager lists the packages that have available updates. To obtaining an update, simply select its checkbox and then click Update.

By default, the NuGet package manager displays only the packages that are available from NuGet.org. If you plan to create or use a custom or licensed package, you can add another package source to the package manager from the Options dialog box shown in Figure 10. To open the Options dialog box, click the gear icon within the package manager. In the Options dialog box, click the Add button (the plus sign) to define a new source, and then configure the name and URL or disk location of the source. After adding the source, you can add packages from the source by selecting it from the drop-down menu in the package manager.

  1. Adding a package source.
    p<. A common scenario is to use NuGet packages without committing the packages that you are using into source control. You can configure Visual Studio to automatically restore packages from the package source server so that the package binaries are not included in source control. You configure this setting on the General tab of the Options dialog box, as shown in Figure 11.
  1. Configuring general package options.

For more-advanced solutions, you can create a custom .nuspec file that defines the framework names. The .nuspec file content would be similar to the following code segment:

<files>
    <file src=”Mac\bin\Release\*.dll” target=”lib\Xamarin.Mac20” />
    <file src=”iOS\bin\Release\*.dll” target=”lib\Xamarin.iOS10” />
    <file src=”Android\bin\Release\*.dll” target=”lib\MonoAndroid10” />
    <file src=”iOSClassic\bin\Release\*.dll” target=”lib\MonoTouch10” />


pre

You can specify package versions in the .nuspec file. This would be necessary if the solution targets a certain minimum version that must be used. For example, in the target fields of preceding code segment, Xamarin.Mac20 specifies version 2.0 and Xamarin.iOS10 specifies version 1.0.

Try Ultimate UI for Xamarin for Free: bit.ly/UltimateXamarin

Implementing grid layouts with Xamarin

Recent mobile content uses square layouts to modernize the user interface. Xamarin has a built-in layout named Grid that arranges content into rows and columns. The Grid layout is useful if you want to arrange buttons or content into rows and columns. Some examples include the numbers in a calculator app, the iOS and Android home screens, or toolbars that have data in equally-sized squares.

The Grid layout is unlike a table in that the content does not determine the number of cells; instead, you specify the number of rows and columns.

The Grid layout includes the following components:

  • Rows and columns
  • Data placement
  • Cell spacing
  • Cell spanning

Rows and columns

The data that defines the number of rows and columns of a Grid layout is stored in the RowDefinitions and ColumnDefinitions collections. Each collection has only one property to configure: rows are defined by the Height property, and columns are defined by the Width property. The Height and Width values can be defined in one of three ways:

  • Auto automatically fits the column or row to its content.
  • Proportional (defined by an asterisk) sizes the column or row to fill the available space.
  • Absolute[* *]defines a specific value for the column height or row width.

For example, imagine that you need to define two rows and two columns of data as follows:

  • The first row should be 100 pixels high.
  • The second row should fill the remaining vertical space.
  • The first column should adjust automatically to the width of its content.
  • The second column should fill the remaining horizontal space.

The XAML code for the layout would be as follows:

<Grid>


pre

By default, Microsoft platforms automatically use proportional widths when using XAML. However, with Xamarin.Forms, the default column width is set to Auto if it is not defined in XAML.

Data placement

After you define the layout of a Grid, you can add children and specify the cell that you want to display the content in. The specific position of the data is defined in the Grid.Row and Grid.Column attached properties for an object. When defining the values for the properties, each row and column starts at zero, so the top left cell of the Grid layout has the position 0,0. Building on the previous example, the following XAML code segment adds label objects at each Grid location:

  <Label Text=”Top Left” Grid.Row=”0” Grid.Column=”0” />