104 – Keeping up to date

Once your PC is set up there are a few things you should do periodically to stay up to date.

Prerequites

James Montemagno, one of the most prolific Xamarin evangelists out there has a great article on preparing your PC for everything that has to be in advance of updating the Visual Studio Xamarin plug-in. I’m not going to plagiarize his work. Go there, read it, follow it, live it, become one with the process of getting all the prerequisites in place before you update the actual Xamarin plugin. Because if you don’t, Visual Studio will become very, very cranky with you; your projects won’t be happy, things won’t compile, and most of all the Xamarin update installer is not smart enough to first check for all the prerequisites and will merrily try to updates when your machine is not ready for and f’up a lot of stuff. http://motzcod.es/post/150380059392/preparing-machines-for-xamarin-cycle

Xamarin plugin for Visual Studio

Once you have run out of things to update on your PC: Android, Java, Windows, Android emulators, other Visual Studio updates, your dog’s shot records… everything… Then it’s time to update the Xamarin environment plugin. From Tools|Options, open the4Xamarin group then select Other. In the right pane, you’ll see a link for ‘Check Now’: Click it. If there is an update to the plugin you’ll be prompted to download and install it. Once the installer is ready to start, close Visual Studio so it doesn’t have to fight to install updates to files that are in use.

Any time you update the environment in Visual Studio you need to update the environment on your Macintosh build machine. They like having synchronized versions so they can keep a good connection, speak the same protocol, have the same expectations etc. The same guidelines apply here: Update everything else first, then update Xamarin.

Open Xamarin Studio on the Mac. Updating the Xamarin environment on the Mac is about the only thing I ever do with Xamarin Studio. From Xamarin Studio menu choose “Check for Updates“. Download all the updates. Click the “Restart and install updates” button. “Restart” in this case is restarting Xamarin Studio, not your entire mac. Even though none of these updates said to restart your mac do it anyway: What does it cost you?

103 – Understanding the Xamarin way

Xamarin as a system will let you build apps specifically for a given platform. If you want to build just an iOS application, you can do that. But its real strength and the focus of this series is on how to make cross platform apps with a single code base. With maybe 5-15% more work & planning you can have 400% more customer potential by having your app run on Android, iOS (including everything from Mac desktops, iPhones, AppleTV and even a little AppleWatch), WindowsUWP (so desktops and Surface tablets alike), and even Windows phone if those 9 users interest you. As of the Connect() 2016 conference announcement, Samsung is also jumping onboard with support for the Tizen operating system used on appliances, wearables and other embedded devices as part of the IoT (Internet of Things) movement.

Xamarin.forms uses XAML markup language for creating the UI (views/pages/controls) and C# for the ‘code behind’; the properties, data, logic and executable code that actually ‘runs’. This separation of UI and code-behind is exactly the same as Microsoft has used for years for their WPF system. Xamarin.forms applications just like WPF applications are primarily architected around the MVVM design pattern. Model View ViewModel is a design pattern that separates data from UI while providing a system for binding that data to the UI in a loosely coupled way so the UI updates automatically when the data changes, and the data is updated when the user makes a change in the UI. The MVVM pattern has proven itself for years in WPF and is well worth the learning curve if you haven’t used it before. If you have experience with WPF/MVVM then you’re going to feel right at home in Xamarin.forms.

A solution consists of multiple projects. 90%+ of your code will be common to all platforms and contained in the Portable Class Library (PCL). This is your UI, logic and models (objects). Then there is a project for each platform. Each of these platform projects will have a file that launches the actual app: That’s just a few lines. Then you might have a bit of code here and there for very specific needs on a given platform. For example, maybe you want to render an octagonal tile for a game you’re making and that level of graphic work is done differently on iOS than Android.

At first there is a little bit of a re-think for experienced desktop program developers when transitioning to having most of their code in a PCL. Since you’re now working in a device-platform-agnostic space you have to consider that your program may launch when you have internet, then loose connection as the device travels. Your program might be running on a landscape 1920×1200 PC monitor or it could be running on an 800×500 low-res smartphone in portrait orientation. You might be running on Wi-Fi, or you might be running on a metered cellular connection where every kb has a dollar figure associated with it. Your app will be launched/backgrounded/resumed/backgrounded/killed at the whim of the user. Even something as simple as file paths and application permissions change between platforms: The path separator for Windows is ‘\’ and on Macintosh its ‘/’.

If there’s any advice I can impart on the subject it’s this: Think generic, plan from the 30,000-ft. view, separation of responsibilities/concerns, code in layers. Let’s say you know you’re going to have some data that you store, fetch, and update. Don’t get bogged down in the details of *how*. You know that C# is an Object-Oriented Programming language. In other words, C# deals with objects, not data. If you’re going to take photos plan to take photos but don’t worry about how to directly access the camera on an Android tablet right off the bat. Just realize you’re going to take a photo at the PCL level (the common, platform-agnostic level) and at some point, that will become a device specific block of code in each platform project that your PCL really doesn’t have to know or care about.

Using a database as a more specific example: At a PCL level your program just needs to know it’s going to save in a database. On iOS, it might be a SQLite database while on Windows it might be a Microsoft SQL database; but the logic of your program shouldn’t be aware or depended on knowing which. The logic tells the Data Access Layer to save. The DAL then calls the specific database implementation to do its job. Maybe that implementation comes from a NuGet package. Maybe it comes from a class you write just for WinPhone. This type of layered approach when combined with good separation of responsibility means if you change choices in databases or you move to a different device you only have to change code in one file without risk of breaking a dozen different classes.

We’ll get into actual code doing this later in the series. For now, it’s just important to grasp the concept and let your brain wrap itself around that for a while. Building software in a layered approach is new to many. Planning for a variety of platforms and screen real estates and hardware capabilities is new to most.

Terminology – The same only different

Windows Forms worked in “Forms”. WPF worked in “Windows”. Xamarin works in “Pages”. A screen of content by any other name is still a screen of content. WPF has a “TextBox” and “StackPanel“. Xamarin has an “Entry” and “StackLayout“. Don’t let yourself get too caught up on the terms. It’s all basically the same thingies you’ve used before, with a new name or slight twist. Some of the twists are actually improvements upon WPF. For example, WPF horizontal text alignment was left, center and right. Xamarin is Start, Center, End. This makes more sense when we consider that not all languages are left-to-right reading. For English ‘Start’ will be left alignment. But for Yiddish, ‘Start’ will be right alignment. With no extra work on the part of the developer.

What sets Xamarin apart is the eco-system under the XAML/C#. Xamarin took the mono port of .NET and hitched it up to their Xamarin.forms XAML implementation on all the popular platforms: Android, iOS, UWP, Windows, Mac. So, when you make an “Entry” in your shared code the mono runtime on iOS will make an iOS native textbox, and on android it will make an Android native textbox; regardless of what those platforms might call them. It maps from a common set of controls to the native controls on each platform – so you don’t have to learn all three. Thus, your program will always be native running code on any device.

You do not build a program “In Xamarin”. You build your program in C# and XAML, using Xamarin.Forms.

102 – Resource for success

The best place to start learning Xamarin is… Xamarin Resources. Go figure. Xamarin even has their own Xamarin University set of courses you can take live on-line as well as pre-recorded classes. Not just on Xamarin, but also C#, F# and a bunch of other stuff. It’s not free – or even cheap. But do you really want a ‘cheap’ education as the basis of your career? I think it was like US$2400/year per person. If you can afford it, it is the fastest and best source available. If you can afford it you will learn 5 times faster and better and that of course has a dollar value to you. If you can learn faster you can be earn faster and that might be important to you. Also, by becoming a Xamarin Certified developer you are granted access to a closed area of Linked-In where companies and developers are introduced. Small companies looking for freelancers, or looking for full-time hire. That alone can be worth the cost.

Remember that Xamarin XAML for building your pages is just a different implementation of XAML, which is the markup language used for WPF. So if you’re not well versed with WPF there is a LOT of free resources for it and it all pretty much applies. Once you have a solid understanding of WPF concepts it translates nicely to Xamarin. The differences are basically “StackLayout” instead of “StackPanel“… “Entry” instead of “TextBox” etc.

The de-facto text/reference book on Xamarin is by Charles Petzold and you can download it for FREE.

Xamarin Forums is a great place to ask questions. I would point out the first step is always to try to learn. Don’t spend 1 day in Xamarin then hammer people with day 1 questions because you’re too lazy to spend some time self-educating. The forums are also a great place to just spend some time reading. Read through the questions others ask because it gives you some insight into problems other had, and how they resolved them. Better to have that in the back of your mind when you get started so you can avoid going down the wrong path.

Stackoverflow has a Xamarin specific area and tag for questions. The good folks at Xamarin monitor it, as well as the general community of Xamarin developers.

Xamarin components – While there are numerous NuGet packages you may want, this collection is always your first stop. It includes components for cross-platform implementation of everyday needs from Compass to File Storage.

Xamarin Bugzilla – If you have found a real bug. A true bug in Xamarin. Not that you can’t figure it out in 5 minutes or it doesn’t work the way you thought/expected. But an honest-to-goodness bug. Here is where you report it. Be sure you have asked around on the Forum first. Be sure you can reproduce it in a small sample, then include the sample in the bug report.

The prep list for doing an upgrade – This step-by-step by James Montemagno will save you lots of headaches.

Visual Studio Documentation at Microsoft

Visual Studio Keyboard shortcuts (2012, 2013, 2015)

Visual Studio (2015) features and tips

The definitive guide to String.Format. Using the right codes such as {0:c} for currency can save you so much B.S. trying to re-invent the wheel when it comes to formatting.

101 – Setting up your environment

Physical environment

Many people consider their development environment to be their PC and the software on it. They overlook the actual physical environment. Take some time to put some planning into this. Odds are you’re going to spend 8-12 hours a day in this space, so it really does matter.

Get the ergonomics right. If your chair isn’t comfortable and promote the right posture for the long haul you’re going to suffer. If your monitors aren’t at eye level you’re going to suffer. If you’re on a laptop put it on a riser so the screen is at eye level and use an external keyboard and mouse or trackball. Use a decent keyboard like the Microsoft ergonomic models to avoid issues like carpal-tunnel.

Spend a couple bucks on extra monitors of decent quality. A couple extra monitors pay for themselves in improved productivity: Not wasting lots of time opening and hiding various pallets due to limited screen real estate etc. Plus you’re probably going to have Visual Studio, email, a task list and a web browser that lives on Google as its home page all open at the same time. That’s not accounting for a device emulator if you need one or two. You can eat up screen space in a heartbeat as a developer.

Quality monitors such as a Dell with good specifications and wide viewing angle will save your eyes and reduce aggravation when compared to low-end monitors like Acer that can only be viewed if you are at the exact right angle; especially if you rotate one of the monitors to portrait so you can have a tall monitor for viewing lots of code at once (hint, hint).

Pet peeve: Developers belly-aching about spending money on software or other aspects of their environment. Get over it. Everyone else pays for their tools: Mechanics, plumbers, electricians.

Windows PC

The Windows machine is where you’re going to spend your day. Make it comfortable. Use a machine with as much processor, ram and SSD space as you can afford. Use a fast SSD for boot and applications. Leave the applications and all their required files on the C: drive. Don’t try to get fancy with moving them to a second drive etc. Microsoft applications and Visual Studio in particular don’t handle that well. Use a fast second drive dedicated to Visual Studio code. Drives are cheap these days. A drive with one purpose in life (your code) shouldn’t be a problem. Do not use the default Visual Studio project path because it’s a freaking mile long and will cause a lot of your files to exceed to file path length limitation. Something like “d:\\” is a good place. Don’t bury your projects. “d:\\Code\Projects\Clients\2016\A\AdvarkMoving\” is going to be a problem. If you’re that busy with work then use contract/ticket numbers: “d:\\556677\” is much better.

If you’re going to be using an Android emulator instead of a physical device you’ll need more RAM. Personally, I’d suggest a physical device as soon as possible. Cheap android tablets are everywhere. Spend $50-100 on one. Your cellular provider will probably GIVE you one with a new phone purchase or contract extension; which you know you are going to do anyway.

All flavors of Visual Studio 2015 include Xamarin, including the free community edition. However, when you run the Visual Studio installer the options are not ticked on by default. Choose ‘Custom’ then go through the options turning on all the options for Cross Platform. Personally, I prefer to download the offline installer and run it from my own drive, than using the web installer. But that’s me. If I ever have to re-install I already have the ISO and don’t have to wait (again) for a 6gig download. This is a really good approach if you are on a metered connection such as satellite internet with monthly data limit.

There are a few plug-ins for Visual Studio that make your life easier. Again, you’re spending 40+ hours in this environment so it’s worth making it nice.

  • XAML Regions, free
  • XAML Styler, free
  • VS Color Output, donation ware.
    • Spend a couple bucks and make a donation to the guy. It’s really nice to have key output standout, like errors be in red. Five bucks for this less than your morning latte.
  • Visual Studio Spell Checker, free
  • Automatic Versions
  • Clar MEF Component Cache
    • This free extension is really important because once in a while Visual Studio can get confused about this cache and give you various problems. This extension forces the cache to clear and thus avoid the issues.
  • ReSharper, paid but so very much worth it
  • TabsStudio, paid but really worth it.

Macintosh build machine

Once you get everything set up, you’re not going to spend a lot of time on the mac. It can be a Mac Mini that you keep on a KVM… It can be a paid Mac time-share… It exists as a build machine to keep within the licensing requirement for Xamarin, and to produce the native Swift code via Apple’s XCode IDE.

  • Update everything on the Mac
  • Install XCode
  • Run updates again
  • Install Xamarin from their installer
  • Run updates again.
  • Run Xamarin Studio and have it check for updates
  • Connect your iPad or iPhone.
  • Run XCode
    • Make a new default project. Run it on the iDevice.
    • Follow all the prompts about activating developer mode etc.
    • Provisioning the device will take place as well.
    • Provisioning tends to be a pain in the arse the first few times. There are articles on-line specifically about this that go into great detail. I’m not going to repeat all of that here.
    • Once you have all that working you should be able to make Xamarin.Forms solutions on your Windows PC in Visual Studio and run them on the Mac. Here’s what I do when I start a new solution in Visual Studio.
      • Make a new XCode solution on the Mac settings the company name and program name: com.mycompanyname.supercoolprogramname
      • Run it on the iDevice just to have it fetch provisioning permissions for that name.
      • Close it.
      • Now on the PC in Visual Studio make your new Xamarin.Forms solution.
      • In the properties for the iOS project set the name to exactly match (case sensitive) the one you just made on the mac. Since the device is already provisioned for that product you’re golden.
    • As a tip though: Provisioning on a per-software basis is case sensitive. Just get in the habit of doing everything in all-lower case and it won’t be a problem.

Networking

Wi-Fi is inherently unstable even under the best of conditions. Have your mac and your PC on the same network switch, preferably on either static IP or reserved DHCP so they always have the same address.

The Xamarin’s Mac Build Agent in Visual Studio will offer up all the Mac’s it finds on your network by DNS resolved name. Don’t fall for it. Add a Mac and give it the actual IP address.

Before going any further, make sure all this works a few times with a bunch of ‘disposable’ default applications. Be sure you can make 10 projects and have them all work, getting a solid grasp on the process.

Other tools

ToDoList

There are lots of programs out there for keeping you on task and helping you keep track of your to-do list. Maybe you’re good with just a simple Notepad document or you love spreadsheets. Maybe you have something else you already use. Personally, I’m a huge fan of ToDoList by AbstractSpoon. First of all its free for a very well equipped lite version. It works in node-subnode paradigm which to me reflects how most projects go. And it has built-in time tracking for each line item: Just click the stopwatch when you start working on that item. This is invaluable for all of us but especially for new developers that aren’t good (or honest with themselves) at estimating their time to complete something. It’s really easy to think it only took an hour to make up a graphic, but forget to include the two hours of back-and-fourth with the client for approval on the final version. If you can’t estimate a task you can’t estimate the entire job and suddenly the kid at McDonalds is making more for taking out the trash than you are as a freelance developer.

Move in

Once you get all that going, take some time to move into your environment. Get all your Visual Studio palettes where you like them. And browser. And Outlook/Office. And other tools. Even with 3 monitors I still have programs stacked behind each other. Do it in a way that makes sense: Have your browser on a monitor not covered by Visual Studio so you can read from one screen and type on the other. Put Word and the code page on the vertical monitor because you can see the entire document that way. Etc.