How to add/embed custom font in xamarin forms

November 26, 2021 0 comments

 Embedding fonts in Xamarin Forms is the best way to use fonts, there is no need to add platform specific fonts and code. Its just adding one file and adding one attribute in shared code; that's it; we are ready to use fonts in our code. 

How to add fonts in Xamarin Forms

 In just three steps we shall add custom fonts in Xamarin forms and use it across all the platforms i.e. Android, iOS and UWP.

Before we get into Embedding fonts in Xamarin Forms, lets create a Xamarin Form project using following steps.

Create New Project - Xamarin Forms

Configure your project

Select Template

After creating project make sure it builds and we are able to see it on Emulator or on Mobile Device

After building the Xamarin Form and running it on simulator, we see simple (default) UI as follows

Simple UI without embedding fonts

For Demo purpose we shall use a custom fonts named - Game Of Squids, you can download it using the link - https://www.dafont.com/game-of-squids.font 

Add the font file (ttf or otf) to our project and mark it as embedded resource

Now to Embed or Add above custom font in Xamarin Forms, use following steps

Create folder structure - Resources > Fonts > GameOfSquids.ttf

 Copy font and then click on properties as shown in following image

Xamarin Forms Adding Font

We have to make sure we select Build Action as Embedded Resource as per following image


If we don't set Build Action as Embedded resource then font added in Xamarin forms will not get applied. We have to make sure Font added in Xamarin forms had build Action set as Embedded Resource.


Add ExportFont attribute in our shared code

We shall add ExportFont attribute to one of the files either App.xml.cs or AssemblyInfo.cs, since the attribute will register it on assembly level, we can put it at any one of the location

Anywhere outside the namespace add following line

[assembly: ExportFont("GameOfSquids.ttf", Alias = "GameOfSquids")]



 AssemblyInfo.cs code changes to following

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
[assembly: ExportFont("GameOfSquids.ttf", Alias = "GameOfSquids")]


Now simply Consume the font in our controls

Check following code, we simply added FontFamily="GamesOfSquids" in the autogenerated MainPage.xaml file

Add custom fonts in Xamarin Form - use fonts


That's it, now if we compile code it and put it on Emulator, we can see the changed font. Default font changes to GamesOfSquids


This is how we add custom fonts in Xamarin form



How to Temporarily disable all foreign key constraints

September 12, 2021 0 comments

 While working with MSSQL and Asp.net Core with Entity Framework, many times we have came across a situation where we have to migrate data from one database to newly created database. e.g. Copying all the data from Production DB to Testing DB. 

  • Using Entity Framework Code First approach, we create database
  • Once database is created, need to copy all the data from MSSQL (Generate script > Dataonly); create Database script for the data
  • Now while running the script generated from PROD db to Test DB (there could me some modifications in test db, some columns are added) , we need to disable foreign key constraint and then re-enable foreign key constraints
 

How to disable all the foreign key constraints in MSSQL and then again re-enable all the foreign key constraints in MSSQL 

We can use following scripts

-- Disable all the constraint in database
EXEC sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT all'

-- Enable all the constraint in database
EXEC sp_msforeachtable 'ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all'

This is the most easiest way to Disable and Enable all the Foreign key constraints on MSSQL.

Calculate next due date based on start date frequency and current date in c#

April 17, 2021 0 comments

During one project development there is one scenario which made me write this post.
Scenario is as follows
Company has clients, each has different invoice start date and invoice frequency. I had to calculate and display next invoice date from current date + next month. i.e. invoice will always be raised in next month of service completion. e.g. Client A has invoice start date 01-Jan-2021 and invoice frequency is 5 months. Date when I am writing this post is 17-April-2021. So now next invoice date for Client A is 01-June-2021. For client B invoice start date is 01-March-2021 and invoice frequency is 3 months, then its next invoice date is 01-June-2021 and so on for all other client. 

To handle this scenarios I have written following function in c# which will always returns next invoice date from Current date. 


public class DateHelper
{
 public static DateTime nextInvoiceDate(DateTime? InvoicingStartDate,
    string invoicingFrequency)
     {
     int occurrence =
     (DateTime.Now.Month - Convert.ToInt32(InvoicingStartDate?.Month)) 
     / Convert.ToInt32(invoicingFrequency);
     
     int restInt = 
     (DateTime.Now.Month - Convert.ToInt32(InvoicingStartDate?.Month)) 
     % Convert.ToInt32(invoicingFrequency);
     DateTime next = InvoicingStartDate.Value;
            int multi;
            if (restInt == 0 && DateTime.Now < InvoicingStartDate)
            {
                if (Convert.ToInt32(invoicingFrequency) == 1 && occurrence < 0)
                {
                    multi = 1;
                }
                else
                {
                    multi = Convert.ToInt32(invoicingFrequency)
                    * Math.Abs(occurrence);
                }
                
            }
            else
            {
                if (Convert.ToInt32(invoicingFrequency) == 1)
                {
                    multi = Convert.ToInt32(invoicingFrequency)
                    * (Math.Abs(occurrence));
                }
                else
                {
                    multi = Convert.ToInt32(invoicingFrequency) 
                    * (Math.Abs(occurrence) + 1);
                }

            }
            return next.AddMonths(multi);

        }
    }
  • How to calculate a future date based on a given date ?
  • Calculate next due date based on start date frequency and current date ?
  • How to nearest future date from given date and frequency ?
  • Best way to calculate next date from start plus frequency
  • How to calculate next meeting date based on given frequency ?