Thursday 19 September 2019

Xamarin.Forms - Implement CI/CD Using App Center

Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.

App Center has multiple services that are most commonly used by mobile developers, where multiple services act as a single integrated product. With the use of a single integrated product, you can build, test, distribute, and monitor your mobile apps, and also implement push notifications.

Connect your repo, build your app. It’s that simple.

Connect to GitHub, Bitbucket, or Azure DevOps and build your app in the cloud on every commit.
Automatically run unit tests, release to testers and stores, or test your UI on real devices.
 


Support Platforms

  • Android
  • iOS
  • React Native
  • UWP
  • Xamarin
  • macOS
  • Cordova
 

App Center Services

  1. Build
  2. Diagnostics (Formerly Crashes)
  3. Test
  4. Analytics
  5. Distribute
  6. Push Notifications
CI - Continuous integration is the process of merging all developers' work into a single main repository.
 
CD - Continuous delivery is a process of automatically building and deploying your Xamarin apps to testers or end-users.
 
More info
 
 

 

Tuesday 26 March 2019

Common Challenges Faced by Xamarin.Forms Beginners and Possible Solutions



1 – I get a thousand error messages at my first build 
This is one of the most annoying challenge a Xamarin Forms beginner can face. You launch your IDE, Select Xamarin.Forms project template, but you can’t run it immediately. This is either due to nuget packages which are not restored or packages which need to be updated. If you face this issue, the first thing to do is to restore all your nuget packages for the solution and update if need be. Check this article on how to restore nuget packages in visual studio. 


2- I can’t compile and run my app!!! 
In some cases, even after restoring packages you may have difficulties compiling and running your app. This is usually because of platform specific packages needed to run the app. For example, after restoring nuget packages,  you may face errors like missing package Xamarin.Android.Support.Design or any other Xamarin.Forms support package issue. Your IDE may complain about Missing References to Dependencies or Packages which are actually PRESENT in your solution Or, after running you app, it emidiately closes. All of these issues and a lot more will hinder compilation. Resolving such issue is not always easy, and covering how to deal with each of them requires me to write a new post so instead, here is a link to solutions to these commonly faced problems. 


3- What the hell is this XAML stuff ? Why does this XAML seem so different from .Net XAML 
When you are a Xamarin.Forms beginner, and new to .Net development too, you can get very confused in the beginning. XAML is a markup language used to build user interfaces in .Net apps. You have the choice to build user interfaces either with XAML or with C# code and you may ask your self if you should use XAML the answer is YES you should. Using XAML allows you to better separate UI code from code logic, and is a better choice when you will implement MVVM. Another difficulty comes when you are from WPF, UWP … background you think the XAML you are used to is another form of XAML, with different names for controls. to overcome this, You should check Xamarin.Forms documentation, and Check this article about XAML Standards. 


4- Where are these Ghost exceptions coming from ? How do I understand them ? 
Another annoying issue is, running your app to later have it throw what I call Ghost Exceptions Exceptions thrown with only a minimum information about their occurrence. For example, only the name of the exception is given, and but no additional information is given. This type of exception occures mostly when there is an error in your XAML code, and since you cannot place break points in XAML, it is a little bit difficult to figure out what the exception is. The best way I overcome this is by launching the app on a different platform, and depending on how the exception is thrown on each platform, you can get enough information about it. I mostly find out that such exceptions are thrown when ever I’m careless in my XAML code. 


5- Where is the designer ? 
When you are from a mobile development background like Android or UWP, you are used to have a designer where you drag and drop views. But in Xamarin.Forms there is no such designer at least not yet and not to my knowledge. This can seem weird at first and even frustrating, check this post from a Xamarin.Forms beginner who poured out his rage after such frustration. Check this article which will tell you more about this. 


6- Debugging Sucks !!! 
This is one of the most common complaints. You want to debug your app, and you find out that it is some times not as easy as what you are used to. Exceptions occur all the time and where in your code the exception occured is not always obvious. Yes, this is sometimes true, but Xamarin.Forms has become a lot better with time, so nowadays it is even easier to debug. My preferred debugging approaches are; When the unhandled exception occurs I read the Inner Exceptions from these exceptions. This almost always give me a clue about the cause and where the exception occurred. Or when an unknown exception fires, with no details on platforms like android, I try to run the app on windows to fire this exception, since in my opinion, windows platform describes better the exceptions which occur. Of course when I use Xamarin.Forms. 


7- Must I compile and run my code every time I want to see my app’s UI ? 
The answer to this is clear and simple. No, you mustn’t. There are several tools available for you to visualize your XAML layout while you code. Some of these codes include the XAML previewer on Visual Studio and Gorilla Player. These tools will make app development in Xamarin.Forms a lot more faster. 


8- MVVM 
When you are new to .Net platform, you may wonder what is MVVM. Because you must have heard about it several times especially if you develop mobile or desktop apps. Though MVVM Architectural Design Pattern is not a must, you should invest time learning it and master a Xamarin.Forms MVVM Framework. Which will make your code more maintainable.And you will have a better time while building Xamarin.Forms apps since it will  avoid you countless troubles related to poorly designed code. 


9- Why does my app not look totally native as it should be ? 
In deed, Xamarin.Forms is a Native cross platform UI framework. But when you take a look at the controls which it provides for each platform, they are not always identical to those you see on the respective platforms. In my Opinion, the order in which most of Xamarin.Forms controls look native on each platform is UWP > Android > iOS. But this does not mean you cannot give this native aspect to your controls. This could be done very easily using custom renderers as described in this documentation. 


10- Understanding the Xamarin.Forms Philosophy 
This is one of the things I find in common with most of the beginners who ask questions. On sites like Reddit or Stackoverflow. They don’t fully the Xamarin.Forms approach to cross platform development. Xamarin.Forms is not a Black Box Tool which you will code everything in C# and Tada!!! is is converted into native Android, iOS … native app. Instead, you should see it as a only a UI Framework. Which provides you a set of controls you could use in one project. And these controls have their own implementation on each platform. The Xamarin.Forms way is very particular, and I think understanding it will help facilitate its use by any new developer. This article deeply explains the XAmarin.Forms way. 
 

Thursday 7 February 2019

What's New in Xamarin Forms?


ThreeFive (Xamarin.Forms 3.5.0)

Release Notes
  • BindableLayout
  • ListView ScrollBarVisibility
  • SwitchCell OnColor
  • ToolbarItem, MasterDetailPage, NavigationPage a11y

ThreeFour (Xamarin.Forms 3.4.0)

Release Notes
  • ImageButton

ThreeThree (Xamarin.Forms 3.3.0)

Release Notes
  • Label & Span Text Decorations: Underline and Strikethrough
  • Label MaxLines
  • [iOS] Modal Presentation Model
  • [iOS] WebView now uses WKWebView

ThreeTwo (Xamarin.Forms 3.2.0)

Release Notes
  • BoxView Corner Radius
  • Button Padding
  • Editor Placeholder and Placeholder Color
  • [iOS] Entry: Position and Color of Caret
  • Label Line Height
  • OnPlatform/OnIdiom Markup Extensions
  • Span Gestures
  • Swipe Gesture Recognizer
  • [iOS] Tap Slider
  • TitleView

ThreeOne (Xamarin.Forms 3.1.0)

On this page I'm looking at a series of F100 features, collaborations with the Xamarin.Forms community.
Release Notes
  • Android Bottom Tabs
  • Android Bottom Tab styling options
  • Auto-Resize Editor
  • Return key setting for Entry
  • AutoCapitalization keyboard flag for Entry
  • Vertical and Horizontal scrollbar options
  • Text prediction enable/disable

Xamarin.Forms 3.0.0 Service Release 4

  • FlexLayout
  • ResourceDictionary Improvements
  • Right to Left Support
  • StyleSheets
  • Visual State Manager


https://github.com/davidortinau/TheLittleThingsPlayground

.NET Standard and Xamarin.Forms for the .NET Developer


Introduction to Xamarin


Tuesday 5 February 2019

SQLite vs Realm vs FCM DB

SQLite - SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation. SQLite supports all the relational database features.

Realm - Realm is a mobile database and a replacement for SQLite. Realm is not using SQLite as it’s engine. Instead it has own C++ core and aims to provide a mobile-first alternative to SQLite. Realm store data in a universal, table-based format by a C++ core. This is what allows Realm to allow data access from multiple languages as well as a range of ad hoc queries.
Below are the advantages of Realm over SQLite:
1. faster than SQLite (up to 10x speed up over raw SQLite for normal operations)
2. easy to use
3. object conversion handled for you
4. convenient for creating and storing data on the fly

Realm Better then SQLLite

FCM database - The Firebase Realtime Database is a cloud-hosted database. Data is stored as JSON and synchronized in realtime to every connected client. FCM does not create its own database on the device but it has an offline feature that lets developers to store any data locally when the device is not connected to internet. Normally REST/SOAP API is created to access or udpdate data in a centralized database. FCM database can help you eliminating the creation of webservice as you can create your database directly on FCM and access it using their SDK.

XamarinForms-Realm  ,C# Corner Link  ,Github Link2

Monday 4 February 2019

Xamarin Forms Dependency Injection



Calling native platform code in your portable class library (PCL) is achievable via Dependency Injection. 

It’s a common question for people starting out, who are using a PCL or .NET Standard Library for developing their Xamarin apps. Dependency Injection involves creating an interface that can be commonly applied across all native platforms, then coding the actual implementation, or the native platform code, on each platform. 



This interface and instance is then given to a container. The PCL or .NET Standard Library, then goes to the container with only the interface known, to retrieve the instance. It doesn’t know where the instance came from, just that it has a specific interface.
Wikipedia definition “ - In software engineering, dependency injection is a technique whereby one object (or static method) supplies the dependencies of another object. A dependency is an object that can be used (a service).
Before understanding what it means in programming, let’s first see what it means in general as it will help us understand the concept better.
Dependency or dependent means relying on something for support. Like if, I say we are relying too much on mobile phones than it means we are dependent on them.
So before getting to dependency injections, first let’s understand what a dependency in programming means.
When class A uses some functionality of class B, then its said that class A has a dependency of class B.



In C#, before we can use methods of other classes, we first need to create the object of that class (i.e. class A needs to create an instance of class B).


So, transferring the task of creating the object to someone else and directly using the dependency is called dependency injection.

Why should I use dependency injection?

Let’s say we have a car class, which contains various objects such as wheels, engine, etc.
class Car{


private Wheels wheel = new MRFWheels();


private Battery battery = new ExcideBattery();


...


...

}
Here the car class is responsible for creating all the dependency objects. Now, what if we decide to ditch MRFWheels in the future and want to use Yokohama Wheels?
We will need to recreate the car object with a new Yokohama dependency. But when using dependency injection (DI), we can change the Wheels at runtime (because dependencies can be injected at runtime rather than at compile time).
You can think of DI as the middleman in our code who does all the work of creating the preferred wheels object and providing it to the Car class.
It makes our Car class independent from creating the objects of Wheels, Battery, etc.

There are basically three types of dependency injection:

  1. constructor injection: the dependencies are provided through a class constructor.
  2. setter injection: the client exposes a setter method that the injector uses to inject the dependency.
  3. interface injection: the dependency provides an injector method that will inject the dependency into any client passed to it. Clients must implement an interface that exposes a setter method that accepts the dependency.we will learn this in detail below using example of Xamarin forms applications.
class Car
{


private Wheels wheel;


private Battery battery;





/*Somewhere in our codebase we instatiate the objects required by this class.


There are two methods for implementing DI:


1. Constructor based


2. Setter based


*/





// Constructor Based


Car(Wheel wh, Battery bt) {


this.wh = wh;


this.bt = bt;


}





// Setter Based


void setWheel(Wheel wh){


this.wh = wh;


}


...


...


// Rest of code

}
So now its the dependency injection’s responsibility to:
  1. Create the objects
  2. Know which classes require those objects
  3. And provide them all those objects
If there is any change in objects, then DI looks into it and it should not concern the class using those objects. This way if the objects change in the future, then its DI’s responsibility to provide the appropriate objects to the class.


Interface

The first thing we must do to enable dependency injection is define an interface. For this example, lets say we have to get a unique identifier for the mobile phone. There are specific calls you can make on a each platform to get this, but its not available in our PCL. Hence we would define this interface to return a string of unique identifier.
public interface IDeviceInfo
{
    string GetUniqueIdentifier();
}
This interface would be placed in your PCL and hence accessible from your PCL and each platform project.

Implementation

Next we need to actually define the implementation of this interface in each platform.

iOS

public class DeviceInfo : IDeviceInfo
{
    public string GetUniqueIdentifier()
    {
        return UIDevice.CurrentDevice.IdentifierForVendor.AsString();
    }
}

Android

public class DeviceInfo : IDeviceInfo
{
    public string GetUniqueIdentifier()
    {
        return Android.Provider.Settings.Secure.GetString(Xamarin.Forms.Forms.Context.ContentResolver,
                                                          Android.Provider.Settings.Secure.AndroidId);
    }
}

UWP

public class DeviceInfo: IDeviceInfo
{
    public string GetUniqueIdentifier()
    {
        var token = HardwareIdentification.GetPackageSpecificToken(null);
        var hardwareId = token.Id;
        var dataReader = Windows.Storage.Streams.DataReader.FromBuffer(hardwareId);
 
        byte[] bytes = new byte[hardwareId.Length];
        dataReader.ReadBytes(bytes);
 
        return BitConverter.ToString(bytes);
    }
}

Dependency Injection Container Registration

Next we want to register this implementation with the Dependency Injection Framework. For this example, I will just use Xamarin Forms inbuilt dependency injection container. Place the following above each implementation of the DeviceInfo, above the namespace declaration.
[assembly: Xamarin.Forms.Dependency(typeof(DeviceInfo))]
namespace Mobile.Platform
{
   ...
You can also register it via a line of code, and this is actually need for UWP, as the assembly registration has issues with UWP under Native Compilation.
Xamarin.Forms.DependencyService.Register<DeviceInfo>();

Retrieve Dependency

Lets now retrieve that dependency in code. All we need to do is call this line, referencing the interface we want the implementation of.
DependencyService.Get<IDeviceInfo>().GetUniqueIdentifier();
Now we can call the GetUniqueIdentifier and it will return a string. The PCL or .NET Standard Library doesn’t know anything about the platform, and just interact with the instance via the interface definition.

AutoFac

The Xamarin Forms Dependency Service is a nice simple dependency injection framework, but it lacks many features, including the ability to do constructor injection. Using something more advanced such as AutoFac offers you many more features. There are many other Dependency Injection Frameworks out there, and they all provide very similar functionality.
To create the container, implement the following code.
ContainerBuilder _builder = new ContainerBuilder();
Now you can add dependencies as needed.
_builder.RegisterInstance<IDeviceInfo>(new DeviceInfo()).SingleInstance();
Finally you need to call the Build function to finish the building of the container.
_builder.Build();
The Container should be accessible in the PCL and native platforms, as is the interface. Similar to Xamarin Forms implementation, you can also get the

Constructor Injection

The benefits of using a Dependency Injection Framework such as AutoFac, is you can inject dependencies into a constructor. For example.
public class MyViewModel
{
    private readonly IDeviceInfo _deviceInfo;
    public MyViewModel(IDeviceInfo deviceInfo)
    {
        _deviceInfo = deviceInfo;
    }
}
If you had registered your ViewModel and DeviceInfo, in AutoFac, when AutoFac creates an instance of your ViewModel, it will automatically detect constructors with parameters and try to fill them in as needed. This has immense benefits for readability and unit testing. No longer is a dependency in your class, hidden via a line of code, its clearly apparent, as you can only create the class, if you inject the dependency into it.

Other Frameworks

You might be wondering what other frameworks are out there. First they are some MVVM frameworks that come with one included such as FreshMVVM and MVVMLight. If you prefer to keep your MVVM frameworks and Dependency Injection Framework separate, you can also use Ninject, TinyIoc, Unity, Castle Project, StructureMap and Spring.NET, just to name a few. You can do some research to find the one that best meets your needs. Personally, I prefer AutoFac, as its recent speed improvements, along with powerful interface, features and maturity make it great to work with.

Inversion of control —the concept behind DI

This states that a class should not configure its dependencies statically but should be configured by some other class from outside.

Benefits of using DI

  1. Helps in Unit testing.
  2. Boiler plate code is reduced, as initializing of dependencies is done by the injector component.
  3. Extending the application becomes easier.
  4. Helps to enable loose coupling, which is important in application programming.

Disadvantages of DI

  1. It’s a bit complex to learn, and if overused can lead to management issues and other problems.
  2. Many compile time errors are pushed to run-time.
  3. Dependency injection frameworks are implemented with reflection or dynamic programming. This can hinder use of IDE automation, such as “find references”, “show call hierarchy” and safe refactoring.
You can implement dependency injection on your own (Pure Vanilla) or use third-party libraries or frameworks.

All About .NET MAUI

  What’s .NET MAUI? .NET MAUI (.NET Multi-platform App UI) is a framework for building modern, multi-platform, natively compiled iOS, Androi...

Ads2