Tuesday, March 15, 2016

Initial Learnings Setting Up For Xamarin

I've been gearing up some work projects as well as some home projects recently to use Xamarin.  Specifically, of special interest to me is Xamarin Forms. In my opinion this is a truly incredibly useful technology.

But first, inevitably, there is some setup and familiarisation to get through before getting into code.  Here are some of my random findings during this process.

The Xamarin trial period is too short.

Its fantastic there actually is a trial period. However there's quite a bit of learning and discovery to do, and 30 days is just not enough.  Its an amazing product well worth the money, so don't be afraid to jump in.  The Xamarin.com web site pricing indicates pricing per month, but you must sign up for a full year however.

The Android SDK Emulator is still shocking.

Glaciers move faster than the default Android SDK Emulator.  There is wide recognition of this, but in the few years I've been involved in mobile development, it hasn't really improved.  The number of how-to web pages describing performance tweaks seems to back this up as well.

TIP: Fortunately there is a decent Andriod Simulator now for Visual Studio.  It can be downloaded and installed separately here.
The good news: It even integrates into Visual Studio Community edition and Xamarin Studio.  The bad news: It doesn't work with operating systems that don't support Hyper-V (which include Windows 10 Home see here).

TIP: Don't close the Emulator when you're finished debugging, running code again with Visual Studio or Xamarin Studio will redeploy and reattach much faster than re-opening it.

Creating New Projects in Xamarin Studio.

Xamarin Studio doesn't set all the same defaults as Visual Studio.  

TIP: I had to manually tick the box to deploy my application when I 'F5' or click the Debug button. The Deploy box needs to be ticked.

TIP: Using File-New-Project in Xamarin Studio doesn't create any Windows flavoured Apps. Also if the Apple build server isn't available at the time, you won't get an iOS App project either.  I've had some trouble using Visual Studio File-New-Project with creation errors and PowerShell errors (which is probably issues with my rig rather than Vs), but Xamarin Studio has worked every time.

Visual Studio vs Xamarin Studio on Windows

I've been impressed with Xamarin Studio so far.  If you're a fan of Visual Studio like me, don't be tempted to write it off.  Xamarin Studio presents a very clean and simple IDE with full intelli-sense support.  I couldn't fault it creating new prototype Apps and debugging worked seemlessly everytime. It even can deploy and debug code to the new Visual Studio Android Emulator. 

On the downside, Xamarin Studio does not have a XAML designer at all, which is a real negative for me.  Another obvious issue is that any iOS Xamarin native development can only be performed on Xamarin Studio on a Mac.  (Any shared iOS/Android PCL development can be done on Windows however). Also, only a Mac can build the native iOS binaries.  Both Xamarin Studio and Visual Studio can use a Mac remotely on the LAN to trigger a build.  The iOS Simulator can only be used locally on a Mac however.

I don'y miss R# that much its stops me from using Xamarin Studio, but any kind of XAML editing I prefer Visual Studio. Anecdotally, I do believe Xamarin Studio performs better than Visual Studio.

Resharper tends to show phantom errors

Not sure what's going on with R#, but with my version, 10.0.2 I occasionally see red squiggly underlines indicating errors on methods that are building and functioning fine. No solution for this so far, but its not a show-stopper for me.

TIP: When building for the first time in Visual Studio, I had some phantom errors showing up which are coming from R#.  To get rid of them I changed the build error filter to "Errors Only"; it was defaulting to "Errors+Intellisense".

Thats all I have for now, more learning still to do, so I'll have more to say in the near future.

Friday, January 8, 2016

Method doesn't throw ArgumentNullException when expected

I noticed an interesting test failure when I refactored a method to use the recently added C# keyword 'yeild'.
    public class Subject
    {
        public IEnumerable<string> DoWork(string foo)
        {
            if (foo == null) throw new ArgumentNullException(nameof(foo));
            var someCollection = Enumerable.Range(0, 100);
            foreach (var number in someCollection)
            {
                yield return foo + number.ToString("C");
            }
        }
    }


The above method is, of course, a contrived and simplified example.
Here is the unit test for this method to check that ArgumentNullException is thrown.

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void DoWork_ShouldThrow_GivenNullFoo()
        {
            var subject = new Subject();
            var result = subject.DoWork(null);
            Assert.Fail();
        }

This test does not pass in its current form. The thread reaches the Assert.Fail() and the test fails.


Assert.Fail failed. 

   at MyAssemblyTest.SubjectTest.DoWork_ShouldThrow_GivenNullFoo() in C:\... line 55
If you're like me and seldom use the yield keyword, it takes a while to realise that the yield keyword triggers delayed execution of the entire method. Meaning the methods doesn't actually run until you access the collection that is returned. When the collection is accessed the exception is thrown.

Here's my modified unit test to cater for this:

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void DoWork_ShouldThrow_GivenNullFoo()
        {
            var subject = new Subject();
            var result = subject.DoWork(null);
            result.Any();
            Assert.Fail();
        }

Sunday, August 9, 2015

Convert existing event handler invocations to C#6 Null Propagation

Search for:

if \(handler \!= null\)\s*\r?\n\s*\{\r?\n\s*handler\((?<args>.*?)\);\r?\n\s*\}
and replace using:
handler?.Invoke(${args});

For example, this will be matched and replaced

if (handler != null)
{
    handler(this, new DialogResponseEventArgs(id, true));
}

with this
handler?.Invoke(this, new DialogResponseEventArgs(this.dialogCorrelationId, message.Response == ShellDialogButton.Cancel));

Converting existing code to use C#6 Expression Bodied Properties

Converting all types of Simple Properties

Search for

[^\S\r\n]+(?<accessor>\w+)(?<override> override | )(?<typename>\w+) (?<pname>.*?)\r?\n\s*\{\r?\n\s*get(\r?\n)*\s*\{\s*return (?<returnexpr>.*?);\s*\}\s*\}
and replace with
${accessor} ${override}${typename} ${pname} => ${returnexpr};

For example the following properties will be matched and converted:

public override string Description 
{
    get 
    {
        return "The quick brown fox jumped over the lazy dog.";
    }
}
public string Description 
{
    get 
    {
        return "The quick brown fox jumped over the lazy dog.";
    }
}
public string Description 
{
    get { return "The quick brown fox jumped over the lazy dog."; }
}
public bool HasValue 
{
    get { return this.field; }
}
public virtual IEnumerable<BudgetBucket> Buckets
{
    get { return this.lookupTable.Values.OrderBy(b => b.Code).ToList(); }
}

All the above are changed to the new syntax:
public override string Description => "The quick brown fox jumped over the lazy dog.";