Learn how to use the Flux design pattern with Xamarin.
Source code: https://github.com/SuavePirate/Xamarin.Flux
Learn how to use the Flux design pattern with Xamarin.
Source code: https://github.com/SuavePirate/Xamarin.Flux
Learn how to use 4 methods to call platform specific code from shared code in Xamarin. Make calls to the HockeyApp iOS SDK from a Portable Class Library.
Source Code: https://github.com/SuavePirate/Xamarin.HockeyApp.Portable
Using all 4 methods to call platform specific code from shared code in Xamarin. Make calls to the HockeyApp iOS SDK from a Portable Class Library.
Source Code: https://github.com/SuavePirate/Xamarin.HockeyApp.Portable
Using Dependency Injection to call platform specific code from shared code in Xamarin. Make calls to the HockeyApp iOS SDK from a Portable Class Library.
Source Code: https://github.com/SuavePirate/Xamarin.HockeyApp.Portable
Using the Service Locator anti-pattern to call platform specific code from shared code in Xamarin. Make calls to the HockeyApp iOS SDK from a Portable Class Library.
Source Code: https://github.com/SuavePirate/Xamarin.HockeyApp.Portable
Using the Xamarin.Forms DependencyService class to call platform specific code from shared code in Xamarin. Make calls to the HockeyApp iOS SDK from a Portable Class Library.
Source Code: https://github.com/SuavePirate/Xamarin.HockeyApp.Portable
Unlike iOS, Android does not ship with a built in launch screen or splash screen, so it looks particularly ugly when we start up a Xamarin.Forms app and wait for everything to initialize.
Here’s a quick tip to add a Splash screen to your Android app while it loads up your Xamarin.Forms Application.
First thing we are going to do is create a new drawable: Resources > drawable > splash.xml
<?xml version="1.0" encoding="utf-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="#3498DB" />
</shape>
</item>
<item>
<bitmap
android:src="@drawable/icon"
android:gravity="center"
android:layout_gravity="center"/>
</item>
</layer-list>
Now we need to create a style that uses this drawable as the background: Resources > values > styles.xml
...
<style name="Theme.Splash" parent="android:Theme">
<item name="android:windowBackground">
@drawable/Splash
</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowIsFloating">false</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:colorPrimaryDark">#FFFFFF</item>
</style>
...
Lastly we are going to create a new Activity, remove the MainLauncher property from our MainActivity, and add it to our new SplashActivity. Our SplashActivity is going to immediately start up the MainActivity, but use our theme to show the drawable as the background.
[Activity(Label = "myApp", Icon = "@drawable/icon", Theme = "@style/Theme.Splash", MainLauncher = true, NoHistory = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class SplashActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
protected override void OnResume()
{
base.OnResume();
var startUp = new Task(() =>
{
var intent = new Intent(this, typeof(MainActivity));
StartActivity(intent);
});
startUp.ContinueWith(t => Finish());
startUp.Start();
}
}
Also note the NoHistory flag which disables the back button from going back to this activity, it will destroy itself on leaving.
Now when we run our app, we should see our splash screen before the app loads up!

Does your app have sensitive information that belongs to your user? If so, you’re probably taking some action to protect it. Storing it with encryption, locking it behind a passcode, using TouchID, clearing their session when they leave the app, etc.
One thing you might not have considered is a vulnerability when using the app switcher. Could someone take your user’s phone and view the sensitive information by just double tapping the home button?
Let’s protect that data. We’re going to put a blurred view over the app whenever the user leaves (or even just hits the app switcher right away), plus it can look pretty cool!
In our AppDelegate.cs, override the OnResignActivation method:
public override void OnResignActivation(UIApplication uiApplication)
{
var window = UIApplication.SharedApplication.KeyWindow;
var blurView = UIBlurEffect.FromStyle(UIBlurEffectStyle.Light);
var blurEffectView = new UIVisualEffectView(blurView);
blurEffectView.Frame = window.Frame;
blurEffectView.Tag = 808080;
window?.AddSubview(blurEffectView);
base.OnResignActivation(uiApplication);
}
This will add our blurred view whenever they leave. Now to remove it when they come back, override the OnActivated method:
public override void OnActivated(UIApplication uiApplication)
{
var window = UIApplication.SharedApplication.KeyWindow;
window?.ViewWithTag(808080)?.RemoveFromSuperview();
base.OnActivated(uiApplication);
}
And that’s it!
Bonus swift version: Override applicationWillResignActive and applicationDidBecomeActive in your AppDelegate.swift.
func applicationWillResignActive(application: UIApplication) {
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = window!.frame
blurEffectView.tag = 808080
self.window?.addSubview(blurEffectView)
}
func applicationDidBecomeActive (application: UIApplication) {
self.window?.viewWithTag(808080)?.removeFromSuperview()
}
For those who just want code: https://github.com/SuavePirate/Xamarin.Onion
Don’t forget:
A strong and scale-able architecture is important in applications, especially in Mobile Apps. APIs and SDKs are constantly changing, new technology is constantly released, and team sizes are always changing. A solid Onion Architecture can save a development team a lot of time by making it simple to change service implementations, restrict access to certain areas, making logic flow easy to follow, and making testing isolated blocks of code easier.
Some of the important topics this will cover:
In this section, we will talk briefly about building useful tests for our solution, and why the Onion pattern makes it easy to break tests out into individual layers.
In this example, we will add a test project whose purpose it to just test the Business layer within our Infrastructure.
Let’s start with by adding a nUnit project to our solution, or by adding the nuget package to a class library. Xamarin has great documentation on this: https://developer.xamarin.com/guides/cross-platform/application_fundamentals/installing-nunit-using-nuget/
In our project, we also want to install MvvmLight, just like in our Client and Platform layers. We will also need to add references to our Domain.Models, Domain.Interfaces, Application.Models, Application.Interfaces, and Infrastructure.Business projects.
In order to test our Infrastructure.Business project, we will need to create mock versions of our Data project. In our test project, we can create Repository implementations with mock data for each set that we need. For example:
MockGenericRepository.cs
public class MockGenericRepository : IGenericRepository
{
private List _data;
public MockGenericRepository()
{
_data = new List();
}
public void Add(T entity)
{
_data.Add(entity);
}
public void AddRange(IEnumerable entities)
{
_data.AddRange(entities);
}
public Task CommitAsync()
{
return Task.FromResult(false); // we don't need to explicitly save changes
}
public Task FindAsync(Func<T, bool> predicate)
{
var entity =_data.Where(predicate).FirstOrDefault();
return Task.FromResult(entity);
}
public Task<IEnumerable> GetAsync(Func<T, bool> predicate)
{
var entities =_data?.Where(predicate);
return Task.FromResult(entities);
}
public void Remove(T entity)
{
_data.Remove(entity);
}
}
and MockUserRepository.cs
public class MockUserRepository : MockGenericRepository, IUserRepository
{
public MockUserRepository()
: base()
{
}
}
Now that we have some mock implementations, we can set up our tests against our Business logic.
UserBusinessTests.cs
public class UserBusinessTest
{
private IUserService _userService;
[SetUp]
public void StartUpIoC ()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
SimpleIoC.Default.Register<IUserService, UserService>();
SimpleIoC.Default.Register<IUserRepository, MockUserRepository>();
_userService = SimpleIoC.Default.GetInstance();
}
[Test ()]
public async void AddUserTest()
{
var result = await _userService.CreateUserAsync(new NewUser
{
Email = "test@test.com",
FullName = "Testy McTest"
});
Assert.IsNotNull(result.Data);
}
}
Now we can test against any of the business logic in our application with a mock layer. The same practice can be applied to test any other layer in the solution as well. The data layer can be tested by mocking the business layer, and so on.
Looking back at all of the components of our Onion Architecture, one might think, “Wow, that’s a lot of code to do a simple task”. It’s important to remember that this architecture is not for every project. It’s focus is on scalability and testability. If your project has the potential to grow into something quite complicated, with many developers involved, this type of solution might work best for you. However, if you’re working on something quick to get out the door, maybe getting right to the point is easier and best for you.
The best parts about the Onion Architecture are its abilities to make drastic changes to tools or services used, without having to rewrite anything but that components implementation as well as making it easy to test individual layers without affecting the others or using real data. It also allows for closer monitoring and management of the codebase; keeping people from making calls directly from one layer to another. The only thing you have to emphasize is, “Are you adding a reference to another project to get something done? If so, you might be doing it wrong”.