Gorilla player for Xamarin – XAML Preview!

The new Gorilla Player is out, and its great!  What is it?  Its an instant XAML previewer for Xamarin Forms projects!  (There is also a XAML previewer that was announced during the Microsoft Build 2016 conference currently only available in Xamarin Studio).  If you’re tired of the long cycle of designing your XAML then having to compile and deploy to your device or emulator just so you can view your XAML, then this is for you.  Gorilla Player allows you to view your XAML without compiling.  To get set up, all you have to do is complete their BETA registration form then download their package that contains the player source files and app.  The app is a desktop app that runs as a service in the background, acting as a server that listens for connections from your emulator.  After you have the desktop app up and running, navigate to the directory of the player solution project, and open it in Visual Studio.  Build the project and deploy it to your emulator.  Start the player app on your emulator.  It will try to automatically connect to your desktop app.  If it can’t find it, you can optionally connect manually.   In Visual Studio you will see a new toolbar option in Tools > Gorilla Player > Connect to Gorilla Player.  Now when you open XAML files in Visual Studio, you will see a Gorilla Player icon on the source page.

GorillaVS

You should now be able to see your XAML in the emulator!  That’s it for simple XAML.  If you have Xamarin Forms ListViews with bindings, then you can set design time data a number of ways, the simplest I’ve found is to include a JSON file in the root of your project called “SampleData.json”.  In the file, set your design time data by including a JSON object with multiple name value pairs, each with their value set to an array.  The name in each name value pair is the title of your XAML page.  Set the value of the name value pair to an array of objects with properties that conform to your model.  This will be the design time data you see in the XAML previewer.  Don’t forget to set the binding in your ListView to  ItemsSource=”{Binding}” instead of ItemsSource=”{Binding Items}” or whatever your model is bound to in the code behind, when you want to preview using the sample data.

[snippet id=”20″]

There is an API for Gorilla Player that allows you to import custom .dlls for views that inherit from them, and more.  Head on over to GorillaPlayer.com to check them out.  Happy coding.

Colorzilla Developer Add-On for Firefox

I found this amazing tool today:  The ColorZilla developer add-on tool for Firefox.  If you’re a web developer, and like browser F12 tools, you’ll love this.  It has an Eyedropper tool that lets you easily get the color of anything in the browser window, then automatically copies it to your clipboard.

EyeDropper

There are plenty of other features too, such as a color palette browser, and a webpage DOM color analyzer, which its pretty cool.  It extracts all the colors for the page in your current tab.

DomAnalyzer

My advice?  Definitely worth installing!

Using Json.NET Serialization Attributes with clean classes

Json.NET is a wonderful library for serializing and deserializing JSON in .NET.  It is immensely popular, and is included in the project template for ASP.NET applications.  Often when deserializing JSON, the JSON data does not map to your classes neatly.  You can use serialization attributes to decorate your class members, which will control how Json.NET serializes and deserializes your JSON.  If you use interfaces to define your objects, you can decorate the implemented class while keeping your core definitions clean.

[snippet id=”15″]

[snippet id=”13″]

The decorated MySuperStation class above allows us to deserialize JSON containing different property names by mapping the JsonProperty attribute while keeping our interfaces free of decoration.

 

Abstract Classes vs Interfaces in C#

When should you use an abstract class, when should you use an interface?  Here’s the lowdown on some of the reasons to choose one over another.  First, take a look at a very simple example of an abstract class and two classes that inherit from it.

[snippet id=”8″]

Setting aside technical reasons, one of the strongest benefits to using an abstract class is the ability to provide an invariant implementation of some functionality across all sub classes, while leaving other implementations up to the subclass.  In our example above, all types of toast must first be toasted in a toaster.  Then, depending on the type of toast, the subclass can add whatever type of spread it wants.

On to the technical side.  An abstract class cannot be instantiated.  It is meant to be inherited.  If a method is not marked as abstract in an abstract class, the implementation belongs to the abstract class.  Abstract methods are implicitly virtual, and are meant to be overridden by the subclass.  They have no implementation in the base class, and therefore no method body, just a signature.  An class can inherit from multiple interfaces, but only one abstract class.  Here’s a more real world example.  Imagine a website back end where we want to collect and process credit cards for online purchases.  We might implement an abstract class such as the following:

[snippet id=”9″]

The abstract PaymentBase class dictates that no matter what way we collect card information in the front end website, we must always process the payment the same way.

Interfaces are like contracts that dictate all the functionality a class must have that implements it.  When you implement an interface, you must define a method body for all the method signatures in the definition.  Interfaces are great for dependency injection, which states that we should always program to an interface, not an implementation.  This is beyond the scope of this article, but warrants further reading if you’re not familiar with it.

Interfaces enable you do decorate additional classes of the same concept, adding functionality.  Consider the example below.  We have a Customer and a LoyalCustomer class.  They both implement the ICustomer interface.  While we can place an order for each type of customer using the same interface, we can apply a discount to a loyal customer.  Meanwhile, all throughout our application, we can use the ICustomer interface anywhere we refer to a customer, and pass either Customer or LoyalCustomer to the method.  This also allows us to extend Customer to additional custom types down the road.

[snippet id=”10″]

That’s all for now.  I hope you enjoyed this post!

 

 

 

 

 

 

 

 

Why is a manhole cover round?

If anyone ever asks you this in an interview, as I’ve had asked of me, they’re probably testing how you think.  I was a bit shocked at first, what does this have to do with code?  Much, as it turns out.  How do you answer this question?  Many of you know the answer, but if you were like me, sadly I did not.  I gave it my best guess.  Reason out loud.  That’s the most important strategy here.  Turns out manhole covers are round so they don’t fall in the hole, and possibly on someone working in the hole.