Xamarin Tools and Hacks!

    Skand Swami

Some quickstart ready to use tools to make you develop easily!

Useful Hacks for Xamarin



Being a Xamarin Developer or just a casual user, one encounters multiple cumbersome errors and issues which can take hours of research to resolve. This occurs due to lack of proper documentations available online to resolve such issues. Sometimes it's hard to pinpoint the exact occurrence of what's causing the problem.

Also, Xamarin being a common platform for development on both iOS and Android makes it hard to implement features which are easily implemented in Android and iOS separately.

So Xamarians brings you useful hacks which could help u solve your problem with ease.

 1. Performing a Break line in while writing text.            


Text="your first text 
 your nextline text"

2. Clicking a view/control lying beneath another view/control

"InputTransparent= true" for the upper view will enable you to access the view located underneath.

3. Implementing Facebook login with your application.     

 A callback needs to be generated in the AppDelegate for Facebook login to work which developers generally tend to miss. 
public override bool OpenUrl(UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
        {
    // We need to handle URLs by passing them to their own OpenUrl in order to make the SSO authentication works.
            return ApplicationDelegate.SharedInstance.OpenUrl(application, url, sourceApplication, annotation);
        }

4. Changing the colour of a custom checkbox in Android.

if (Android.OS.Build.VERSION.SdkInt > Android.OS.BuildVersionCodes.Kitkat)
  {
       Control.ButtonTintList = Android.Support.V4.Content.ContextCompat.GetColorStateList(Context,             
                             Resource.Drawable.radio_button_tint_color);
   }

5. When the following error occurs. 

Xamarin.Forms.Platform.Android.AppCompat.ButtonRenderer.UpdateBitmap() in Xamarin.Forms.Platform.Android.AppCompat.ButtonRenderer.UpdateBitmap() in <556464d784854d58957ce21f4c5eb182>:0


Above error due to image file not exist which is used in Button.

 6. When the Visual Studio Emulator is running but, does not deploy the solution.

Solution:-

Step 1:

Go to Registry Editor and follow the following path:-

HKEY_LOCAL_MACHINE/SOFTWARE/WOW6432NODE/Android SDK Tools

Step 2:

Change the Path to:-

C:\Users\<Username>\AppData\Local\Xamarin\MonoForAndroid\AndroidSDK

Or wherever the Android SDK is installed in the system.

7. When the camera does not work even though all the permissions are provided. It may show the following errors.



{Java.Lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.mediatek.camera/com.android.camera.CaptureActivity clip={text/uri-list U:file:///storage/emulated/0/Pictures/PubWorks/Pictures/c3c5a1ccc2554a3aa0832a0de2fb221f.jpeg} (has extras) } from ProcessRecord{d7ddddc 17117:com.pubworks.mobilev2/u0a146} (pid=17117, uid=10146) with revoked permission android.permission.CAMERA



Solution:- Use Intent intent = new Intent(MediaStore.ActionImageCaptureSecure);
                Instead of Intent intent = new Intent(MediaStore.ActionImageCapture);

8. Converting Timespan to string:-


public static string TimeSpanToString(TimeSpan? timeSpan)
        {
            DateTime dateTime = DateTime.MinValue + timeSpan?? DateTime.Now;
            DateTimeFormatInfo dateTimeFormat = CultureInfo.CurrentCulture.DateTimeFormat;
            string shortTimePattern = dateTimeFormat.LongTimePattern.Replace(":ss", string.Empty).Replace(":s", string.Empty);
            return dateTime.ToString(shortTimePattern);
        }