Sunday, April 3, 2016

Floating Action Button in Xamarin Forms

I'm loving experimenting with Xamarin Forms at the moment. So when I wanted to include a Floating Action Button (FAB) in Android versions of a Forms App, I naturally wanted to try to get this working with Forms first.  I realise you can use Xamarin Native Android views in a Forms App, but I enjoy XAML and creating reusable controls.

My first port of call was James Montemagno's GitHub and blog and found his examples for a FAB.

https://github.com/jamesmontemagno/FloatingActionButton-for-Xamarin.Android
He does mention he ported this code from: https://github.com/makovkastar/FloatingActionButton

This looks perfect, but I wanted to improve the Forms example to be mostly XAML based and try to best encapsulate the events into the FAB control to minimise the amount of work the consuming developer has to do.

Here's my code so far.
https://github.com/Benrnz/XamlFloatingActionButton

Its far from perfect, but works great.  Be aware however, that if your ObservableCollection has duplicate values in it, the FAB will exhibit unexpected hide/show behaviour.

Here's what I ended up with in my XAML:

  <AbsoluteLayout VerticalOptions="FillAndExpand"  
           HorizontalOptions="FillAndExpand">  
     <StackLayout AbsoluteLayout.LayoutFlags="All"  
            AbsoluteLayout.LayoutBounds="0,0,1,1">  
       <Label Text="FAB Test 1"  
           VerticalOptions="Center"  
           HorizontalOptions="Center" />  
       <ListView x:Name="MainList"   
            ItemsSource="{Binding MyItems}"  
            VerticalOptions="FillAndExpand"/>  
     </StackLayout>  
     <fabTest1:FloatingActionButtonView x:Name="Fab"   
                       AbsoluteLayout.LayoutFlags="PositionProportional"   
                       AbsoluteLayout.LayoutBounds="1,1,AutoSize,AutoSize"  
                       ImageName="ic_add.png"  
                       ColorNormal="#ff3498db"  
                       ColorPressed="Black"  
                       ColorRipple="#ff3498db"  
                       Command="{Binding FabExecuteCommand}"  
                       ParentList="{Binding Source={x:Reference MainList}, Path=.}"/>  
   </AbsoluteLayout>  

Microsoft announces new licencing for Xamarin!




Since Microsoft bought Xamarin and the deal was officially signed and closed about two weeks ago, Microsoft have announced their intentions to open source and make Xamarin free.

Visual Studio Community edition (the free edition) will also have access but not to all features.
This will include:

  • Xamarin Studio (Mac and Windows version)
  • Xamarin Platform for iOS, Android and Windows, for small teams and non-Enterprise organisations.  Enterprise is defined as organisations earning more then one million US Dollars per annum.
  • Check out this blog from https://blog.xamarin.com/xamarin-for-all/
Writing an App for any platform using Xamarin is now totally open source!


Check out the actual //Build 2016 announcement by Scott Guthrie:
https://www.youtube.com/watch?v=WC7ijoFzjEg
18:35

Test Cloud will continue to be a paid product with several tiers although Enterprise MSDN subscribers will get a 25% discount.

Xamarin Insights will merge with Microsoft's HockeyApp over time.  Right now Insights has features that Hockey does not yet support.

Some level of Xamarin University will be included for MSDN Professional and Enterprise subscribers, although this isn't clear yet.

Check out https://store.xamarin.com/ for latest details.

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();
        }