Friday, August 18, 2017

Xamarin Forms: Floating Label Entry View Control

Following on from some of my other posts on tweaking some controls for xamarin forms (like the ToolbarItem and GridView), I thought it would be handy to implement something along the lines of the floating label pattern as outlined here by Brad Frost.
Here’s a look at the output on Android, iOS and Windows:
Don’t get confused: there are labels on the UI to output the binding that the entry controls have – download the code and run the samples.
image
The idea was to have a single control that contained both the label and entry (the placeholder animates out into the label when text replaces the placeholder value)
Here’s what using the xaml looks like:
image
As per usual, you can check out all the source code here
In the future I hope to add some things like validation support. Other options are also perhaps some of the nifty image support that was recently posted by Charlin here
Happy Coding!

Daily Links 18 Aug 2017

Transient Fault Handling in Xamarin.Forms

Multiple Android builds on Xamarin Forms

Mobile Database Bi-Directional Synchronization with a REST API

Ethereum smart contracts in a nutshell for hackers

Managing Data in Microservices

Image Entry in Xamarin Forms

Xamarin Forms Async Task On Startup

Introduction to the ApiExplorer in ASP.NET Core

MOVE FAST, DON'T BREAK YOUR API

Insomnia: Free REST Client

The Visitor Pattern Explained

Web Applications with ASP.NET Core Architecture and Patterns guidance

Quick and Easy ASP.NET Identity Multitenancy

ASPNET-Core-2.0-Stripping-Away-Cross-Cutting-Concerns

GraphiQL in ASP.NET Core

The GitHub GraphQL API

Airbnb Javascript Style Guide

Exploring refit, an automatic type-safe REST library for .NET Standard

LiveXAML and Xamarin.Forms


Tuesday, July 11, 2017

Mobile Cross Platform Image manipulation with Xamarin (Android / iOS / Windows UWP)

There are some common image functions I tend to need a lot with mobile apps.
These are:
  1. Resizing images – usually with a specific size in mind
  2. Knowing what size and existing image is
  3. Converting an image between png and jpeg formats
I recently particularly needed these kinds of functions for including images in pdf documents (more on cross platform pdf generation here)
All the source code for the above can be found here, and the nuget package is available here.
All the code assumes images are in byte[] format – what you’d normally use for storing images in database repos etc.
As an aside – there’s a separate Xamarin Forms Value Converter nuget for binding a byte[] image to an ImageSource here
For all the code, you can choose to use the command interface, or the ImageTools interface, which wraps the functionality in explicit method calls.
     image

Resizing an Image

This is the most common requirement for image manipulation.
Images taken from disc, or using the camera are usually way to big to be included for wire transfers or light weight repo stores. Other usages are perhaps generating a pre-defined size thumbnail image.
IResizeImageCommand resizeCommand = DependencyService.Get<IResizeImageCommand>();  
ResizeImageContext context = new ResizeImageContext  { Height = 130, Width = 130, OriginalImage = image  };  
var resizeResult = await resizeCommand.ExecuteAsync(context);  
if (resizeResult.IsValid())  
{  
   image = resizeResult.ResizedImage;  
}  

Analyze an Image

Sometimes you just want to know what the dimensions of an image is, or what size it is, or in what orientation:
 var analyseImage = DependencyService.Get<IAnalyseImageCommand>();
var analyseResult = await analyseImage.ExecuteAsync(new AnalyseImageContext { Image = imageAsBytes });
if (analyseResult.IsValid())
{
   var imageWidth = analyseResult.Width.ToString();
   var imageHeight = analyseResult.Height.ToString();
   var orientation = analyseResult.Orientaion.ToString();
   var size = analyseResult.SizeInKB.ToString();
}

Convert an Image (Png and Jpeg support only)

I needed this recently because the pdf library I was using only supported jpeg files.
var imageTools = DependencyService.Get<IImageTools>();

var convertResult = await imageTools.ConvertImageAsync(new ConvertImageContext(Logo, ImageFormat.Jpeg));
if (convertResult.TaskResult == TaskResult.Success)
{
   ConvertedLogo = convertResult.ConvertedImage;
   ConvertedSize = ConvertedLogo.SizeInKB().ToString();
}
Here I’m using the ImageTools dependency – you could just as easily have used the command as in the first two examples.
…. I haven’t tested all the platforms extensively, so if you find a bug, feel free to log an issue on github, or better yet: a pull request with the fix.

Thursday, July 6, 2017

Generate pdf documents for iOS, Android & Windows UWP using Xamarin Forms and Syncfusion Essentials–Part 2

Part 1 of the post can be found here
There I talk about getting started, and generating a simple pdf invoice.

The updated source code is in the same repository here
Code of interest is in the GenerateInvoiceCommand, the PdfGenerator & PdfExtensions

Today, I’m going to be talking about generating line items for the invoice. Here’s a screenshot of the generated result on all three platforms:
image
    
With Syncfusion Essentials Pdf, there are two main options for this: PdfLightTable & PdfGrid

After having implemented both, I don’t really see why both exist – the effort and functionality is about the same for both options.
I can say though, that PdfGrid definitely seems the more stable of the two – I found a few bugs in PdfLightTable along the way.

A look at the result: PdfLightTable on the left, and PdfGrid on the right

imageimage

Some things to consider (included in the sample code above)

More documentation on pdf grid and light table can be found here

Adding data

Both types used direct data-source methodologies.

Grid:
image

LightTable:
image

Sizing columns to content

Unfortunately this isn’t as simple as it should be. Static widths are easy – content not so much.
I created extensions for both LightTable and PdfGrid here (currently LightTable doesn’t appear to work)
Currently we have to cycle through all the column data, and measure which is the longest text, and then set the width to the max content width, hence the need for data, page width and font used.

image

Styling the table

PdfGrid has some built-in styles, as well as customization
image

LightTable you can customize the same as with PdfGrid – I created an extension to apply the most common properties to mimic the built-in style of the PdfGrid
image

Paginating the data

This applies to both table types, although again, LightTable seems to have some trouble honoring the PageinateBounds property – which is needed to the table doesn’t render over any footer data.
image

Drawing the pdf document

image

simple enough.

There’s tons more functionality in syncfusion’s pdf library – go check it out.

Clone the repo and play around with the code - and log an issue if you find any bugs in there :)

And don’t forget syncfusion’s awesome community license that  offers their controls free for indie hackers and small businesses.

Tuesday, May 16, 2017

Xamarin Forms: GridView Control

So I was busy with my nightly dev sessions, building the next big startup product [read side-project], when I ran into what I thought was a fairly simple requirement:
I wanted to show a list of stuff in a template of an item that was in a ListView.
image
Turns out hosting a ListView inside a ListView doesn’t work out so well. Besides it being a rocket ship when I only needed a small car, it just doesn’t work. You see, the ListView has some sizing issues that doesn’t make it a great candidate for said scenario.
Now unless I’m missing something here (which is quite likely), I couldn’t really find any Xamarin Forms control that hosted a simple list of items, without needing properties like grouping, selecteditem etc.
After some searching on the interwebs, I came across this really simple example of what I needed from Chase Florell. (I’ve since happened upon this contribution by Daniel Luberda, which also looks pretty decent). It met my very basic requirement of a simple list of stuff, minus some handy bindings, so I set out to edit the code.
You can see the results (source code and sample project) here
Basically, you can create a tile-like layout with N many columns. The content of the “tiile” can be any template you like.
Sample Xaml:
image
The control also supports ItemTappedCommand, which will call a delegate and pass the selected data item as a parameter.
The default for MaxColumns is 1 – which will give you a simple list as illustrated at the top of the post. More columns will give you something like:
imageimage
I wouldn’t use this implementation for more than about 20 or so data items, as there is no cell recycling as you would get in a ListView, but for very basic scnenarios, I’ve found this simple implementation good enough.
As a side note, I’ve also moved the previously mentioned Bindable IsVisible ToolbarItem control to the linked repo, in addition to a few other very basic controls like an Editor that expands as you type, a ListView with ItemClickCommand and default RecycleElement mode, a floating label pattern Entry (pending), and a simple WrapLayout (which I’ve used in combination with an AddStringEntry to create a Tag List)  - which I’ll cover in subsequent posts as I find the time.
Note: FlexLayout & RelativeLayout are new controls coming in Xamarin Forms 3.
Xamarin FTW Smile

Saturday, March 11, 2017

Essential Visual Studio 2017 Tools & Extensions

Web Essentials 2017
Wrapper for many handy extensions such as File Nesting, Image Optimizer, Markdown Editor, Open Command Line & HTML Tools

Productivity Power Tools 2017
Another wrapper for extensions like Ctrl+Click GoTo Navigation, Power Commands for Visual Studio & Copy As Html

XAML Styler 
Format your XAML on save - consistent and readble XAML Style implementations

CodeMaid
Cleanup and format your code. Multiple language support

NUnit 3 Test Adapter
Run NUnit tests in Visual Studio
...also consider xUnit unit testing

Cloud Explorer for Visual Studio 2017
View your cloud resources, inspect properties, and perform key developer actions from within Visual Studio

ResXManager
Central access to all ResX-based string resources in your solution

..any other essential extensions you can't do without? Share in the comments below.





Saturday, March 4, 2017

Xamarin Forms: ToolbarItem with Bindable IsVisible Property

It's fairly typical for toolbar items to change depending on the state of the model that the view is bound to (quite another thing if it's considered good UX).

Unfortunately Xamarin Forms doesn't have an IsVisible property on it's ToolbarItem control, so I had to hunt for a sample implementation.

A found this handy thread on the Xamarin forums.
Basically create a custom control that inherits from ToolbarItem, add a bindable IsVisible property, and add /remove the toolbar items as the property changes.

I made some minor modifications, and added the source code here

Happy Coding :)



Daily Links 4 Mar 2017

The 12 Factor App

Building Single Page Applications on ASP.NET Core with JavaScriptServices

Realtime Databases with the Realm Mobile Platform

Penny Pinching in the Cloud: Running and Managing LOTS of Web Apps on a single Azure App Service

Xamarin Forms ListView Grouping

Xamarin.Forms Android Selected & Unselected Tab Colors

Xamarin Forms Tab Badge

Azure App Service Secrets and Web Site Hidden Gems

What is scope and closure in JavaScript?

Writing Jasmine unit tests in ES6

Getting Started With WebAssembly in Node.js

Parsing And Serializing Large Objects Using JSONStream In Node.js

Azure App Service Authentication in an ASP.NET Core Application




Friday, January 6, 2017

Sentinel Mutant Registration Facial Recognition System with Xamarin Forms and Microsoft Cognitive Services



First off, this is not a step by step tutorial for adding facial recognition to your cross platform xamarin mobile apps. For that, head on over to a blog post from Pierce Boggan: "Adding Facial Recognition to Your Mobile Apps". For another more in-depth article, head on over to "Cognitive Services - Face and Emotion Recognition in Xamarin.Forms with Microsoft Cognitive Services" from Alessandro Del Sole.

What it is, is a fun app that implements some of the face api from microsoft cognitive services - where you can subscribe to really nifty api such as face and emotion recognition. The free plans are really generous - 30k calls per month for the face api.

First and foremost, all the code can be found on github here.

Displaying Screen Shot 2017-01-06 at 5.55.11 PM.png

Introducing: Version 1 of the sentinel mutant registration program

Related image
Because of Xamarin Forms, the sentinels can be running Android, iOS or Windows. The sentinels above look like they prefer Hololens.

All the MCS Face Recognition related code can be found in the share library FaceApi folder.
You will need to implement your own ApiKeyProvider class that supplies the MCS Face Api key that you have registered.

What can you do?

  1. Clear all face registrations. This will call the DeletePersonGroupCommand, which basically deletes all face recognitions within an identified group (in this case: all the mutants)
  2. Register a new mutant. You can do this in one of two ways. Scan their face using the onboard camera, or select from a picture the device has on file. This is done via the RegisterFaceCommand
  3. Identify a mutant. Does your sentinel program need to recognize mutants for capture - this function will scan a face using the camera or file on device, and confirm whether the mutant has been registered or not. This is done via the FindGroupPeopleCommand
  4. Scan mutant. Scan a face for facial attributes such as age and  gender. Done via the DetectFaceCommand
Browse the code and add your own sentinel directives. Just don't tell Mr Eisenhardt of it's existence, lest he and the Brotherhood switch the code to hunt humans ;)