Friday, September 11, 2015

Using the Visual Studio Android emulator inside VMWare

I do my development in Virtual machines.  It makes it much easier to to back up and transfer my dev environments.  However not everything works inside a virtual machine :(.  Emulator that themselves rely on a virtual machine are problematic.

When Visual Studio 2015 was released, I built a new VM and installed everything.  VS2015 worked fine but the new shiny Android emulator wasn't so happy.

The first error was around Hyper-V.

Visual Studio Emulator for Android
The emulator is unable to verify that the virtual machine is running:
Something happened while starting a virtual machine: 'VS Emulator 5-inch KitKat (4.4) XXHDPI Phone.bob' failed to start. (Virtual machine ID 889F3EA2-7B0E-4873-9180-C765E4293D4E)
The Virtual Machine Management Service failed to start the virtual machine 'VS Emulator 5-inch KitKat (4.4) XXHDPI Phone.bob' because one of the Hyper-V components is not running (Virtual machine ID 889F3EA2-7B0E-4873-9180-C765E4293D4E).

This a fairly common error and once that is easy to fix.

  1. Open the .vmx file in notepad and ad the following lines:
    hypervisor.cpuid.v0 = "FALSE"
    mce.enable = "TRUE"
    vhu.enable = "TRUE"
  2. Run the VM.  Go to "Programs and Features" and ensure that Hyper-V is installed



Running the emulator now gives a completely different error.  Yay, progress.
Visual Studio Emulator for Android
An OpenGL error has occurred:
Failed to create Context 0x3005
The emulator will now shut down.

There are multiple ways to fix this:


Either way works:











Friday, September 20, 2013

Slides - Cross platform programming with .net, xamarin and MvvmCross

The slides from yesterdays presentation "Hot tuna - Cross platform development with .net, Xamarin and MvvmCross" are available from here.

Useful links from the presentation

MvvmCross
Stuart Lodge - MvvmCross Author

Twitter          
@slodge

Blog           
http://slodge.blogspot.co.uk/

Videos           
http://www.youtube.com/user/MrHollywoof?feature=watch
Video index   
http://mvvmcross.wordpress.com/

Repository        
https://github.com/slodge/MvvmCross

Xamarin
Website
http://www.xamarin.com/
Samples
https://github.com/xamarin
Mobile library
https://github.com/xamarin/Xamarin.Mobile 

Ninja Coder
Adrian Sudbury – Ninja Coder Author
Twitter       
@asudbury

Blog        
http://adriansudbury.blogspot.co.uk/

Ninja Coder    
http://visualstudiogallery.msdn.microsoft.com/618b51f0-6de8-4f85-95ce-a50c658c7767



Portable Class Libraries
http://www.hanselman.com/blog/CrossPlatformPortableClassLibrariesWithNETAreHappening.aspx

Wednesday, March 27, 2013

Using sqlite.net with POCO classes



SQLite has become one of the most pervasive embedded databases around.  It's built into Android, iOS and OSX and is a part of many applications.  It has also become the recommended client side database for WinRT applications

The go-to solution for using SQLite in .net is sqlite.net.  It’s a simple ORM that comes as one (or two if you want async support) source files.  You can get the full source from github or just the main files from Nuget

Sqlite.net lets you store nearly any object in the database without needing to descend from a specific type.  However to make it work well, you need to decorate your objects with data attributes denoting primary keys, indexes and so on.

For example:

       public class Valuation
       {
             [PrimaryKey, AutoIncrement]
             public int Id { get; set; }
             [Indexed]
             public int StockId { get; set; }
             [Indexed]
             public DateTime Time { get; set; }
             public decimal Price { get; set; }
       }

On startup, you register your class with SQLite as follows:

       var myConnection = new SQLiteConnection ("Stocks.db");
       myConnection.CreateTable<Valuation> ();

This will check to see if the table exists in the database and create or update it if required. 
You can then start using the database in your code.

       var valuations = myConnection.Table<Valuation>().Where(...);

However for my use case (cross platform application with data objects defined in a portable class library and used in WPF, WinRT and MVC applications), the attribute approach didn’t work.  The nice thing about open source though is that you can always change things…

The latest github version of sqlite.net now has an optional argument for CreateTable that allows defining indexes by convention.   

Acceptable values are:
    [Flags]
    public enum CreateFlags
    {
        None = 0,
        ImplicitPK = 1,    // create a primary key for field called 'Id'
        ImplicitIndex = 2, // create an index for fields ending in 'Id'  
        AllImplicit = 3,   // do both above

        AutoIncPK = 4      // force PK field to be auto inc
    }

So to define and register a class, we can now use something like the following:

    public class Valuation
    {
        public int Id { get; set; }
        public int StockId { get; set; }
        public DateTime Time { get; set; }
        public decimal Price { get; set; }
    }

And then 

       myConnection.CreateTable<Valuation>(CreateFlags.AllImplicit | CreateFlags.AutoIncPK);

This will create the table, make Id into an auto incrementing primary key and add an index for StockId.  To explicitly add additional indexes, use the CreateIndex methods.  E.g. 

       myConnection.CreateIndex<Valuation>(v => v.Time);


The main advantage of this approach is that it lets you separate storage details of the object from it's definition.  This is great for cases when you don't want to, or can't, use the sqlite attributes in the main class.  

The main disadvantages are that you have now separated storage from definition, and currently there is no way to set the field size.

Sample applications demonstrating both approaches can be found on github.  The sqlite.dll binaries can be downloaded from here.

Thursday, August 4, 2011

Delphi for iOS!

I attended the Rad Studio World Tour today in Auckland. Delphi XE2 has some nice features (x64, OSX, FireMonkey) but the standout for me was the iOS support.

Developing for the iPhone et al is mostly a pain in the proverbial. XCode is somewhat of a mess and Objective C was designed by someone with an unholy fetish for square brackets. The last time I did iPhone dev, I did most of my coding in c++ on Windows and only booted into OSX for deployment and testing on the iPhone.

Embarcadero are looking to fix that with Delphi XE2. You can write and test your code in Delphi on Windows. When you need to try it on iOS, you create a xcode project (1 mouse click, only needed once) and then boot into OSX and open the xcode project there. From xcode you can edit, compile, run and debug your 100% Delphi code. If you have either Windows or OSX in a virtual machine you can flick from one tother as you wish. Yor app can be compiled and run in both Windows and iOS.

Awesome!

It's not all perfect, xcode is still there, OSX is a must and the whole code signing is probably as irritating as before, but it's much better than the objective c alternative. It only works with new apps written using FireMonkey but you will be able to pull in older code.

The iOS app is full native code, with access to hardware such as gps, accelerometer and camera.

Note: Accessing the phone hardware means that your app will no longer run under windows due to either the hardware or the support units not being there. I suspect that this is resolvable with some conditional defines and a bit of hacking.

Much to my supprise I am now excited again; both about delphi programming and about iOS programming.

Saturday, June 11, 2011

Delphi image components, ImageEn has a new home

If you have used image editing and manipulation in delphi, there is a good change you have used or heard of ImageEn components. I have used them in my photo printing program, Pics Print.

The ImageEn component suite has now moved to delphi shareware company Xequte. You can find them at www.xequte.com/imageen or at www.imageen.com. If you want to see the components in action, download demos from here.

Wednesday, March 9, 2011

Helpful tips for US based developers AKA Short date time is evil

English is a bit like html. There is a standard, but not everyone understands it, and there are a number of slightly incompatible implementations.

This is most obvious in spoken English where accents, and idioms vary wildly causing mutual incomprehension.

The last time I was in the USA, I had the following conversation in a restaurant:

Me: Can I have a coke please

Waitress: I’m sorry, what did you want?

Me: A coca cola please

Waitress: I didn’t quite get that. What did you want?

Me (using the British technique of speaking slowly and loudly so the natives understand): A COCA COLA PLEASE

Waitress (getting quite testy): What?

Me: Can I have a Pepsi?

Waitress: I’m sorry, we don’t have Pepsi. Would you like a coke?


English as she is wrote.

The problem with the World Wide Web is the worldwide part of it. Whatever you write, will probably be read by people from a different country. Even if your audience is English speaking, there are enough differences between countries for confusion and irritation to creep in.

A few examples:

  • Last year, Steam had a pre release offer for a game due to be released on 10/12/10(or similar). When is that? My natural reading was Dec 10, but it may have been Oct 12. There was no way of telling without trying to divine the writer’s intentions and country of residence.
  • Last month, I evaluated several rss readers for Win Phone 7. The first one I looked at had the dates hardcoded as mm/dd. If the author had just used the standard system formatting, it would have been correct and less work.
  • I also looked at The Weather Channel for Win Phone 7. There was a setting to use metric units. However even with this selected some (but not all) temperatures were shown in Fahrenheit. As an added bonus, the tile would sometimes show a temperature of -18. Hawkes Bay hasn't seen -18 since the last ice age.
  • In Blogger post options, the Post date is in m/d/yy format


So what do (mostly)US websites and applications frequently get wrong?

Dates

Short date time is evil! Repeat that and make it a mantra.

Most of the countries I have visited use dd/mm/yy. The USA uses mm/dd/yy. I believe some countries use yy/mm/dd, but don't quote me on that.

The upshot is that a date such as 09/10/11 is entirely ambiguous without context. Even if the date has some hints, such as 09/17/11, I still have to stop and parse the date rather than reading it. When reading, I devote less than a 1/10 of a second per word. Having to stop, figure out the date, and then start up again seriously screws up my reading.

It's even worse when entering dates, now I have even more opportunity for screwing up. Fortunately most sites have moved to date time pickers or separate day/month/year boxes. Not all have though, one site I was on today used a single edit box with a helpful "dd/mm/yyyy" label beside it.

Blogs and articles frequently refer to seasons. I.e. the new gadget is due in Summer 2011.
This is still problematic; our seasons are about 5.5 months out from the USA so that requires a certain amount of adjustment.

As for the holiday season, WTF? I had to ask an American friend for clarification.

"in general US holiday season means from Thanksgiving (3rd Thursday in November) to the day after new year’s day".

That's not what I would have guessed. If we had a holiday season here, it would start just before Christmas and extend till late Jan. No-one would release anything then, we are all on holiday.

Measurement units
When it comes to measures, there is the US still soldiering on with the Imperial system, and pretty much everyone else is on metric. Unfortunately many sites and applications use just the Imperial measures which is at best an aggravation and at worst requires a quick unit conversion with Google, or more likely, a move onto a different site/app.

Now I understand feet and inches in small quantities provided I don't need to be accurate. Miles require a mental conversion so it has to be something important before I will bother with the maths. After that, I get lost quite quickly.

Ounces are awkward. To my uncertain knowledge, the only people around here who still use ounces are those engaged in buying and selling small amounts of dope. This is not at all useful in performing conversions. Update: I have been reliably informed that the local drug dealers have moved on and now sell in metric units and/or dollar units (e.g. a $250 bag). Pounds are the same as ounces, only more so.

Don't get me started on fluid ounces. I have absolutely no idea how big a fluid ounce is, and no desire to know. This has caused me some aggravation in the past; "How big a drink should I have? Well 60 oz sounds small..."

Number formats
Most countries use "," as the thousand separator and "." as the decimal separator. A few however have it the other way round. Some use spaces for a separator, and the Romans don't even use Arabic numerals.

What to do?
  1. Don't hard code date formats. I use 'dd mmm yyyy' in written text. In my web applications, I mostly use 'dd mmm yyyy'. When I use the system short date time then I make sure that I also use asp.net globalisation settings.
  2. Don't hard code number formats either.
  3. Test with multiple cultures. I generally test with NZ and US cultures for dates. I also use Norway or Sweden for testing number formatting.

Why am I picking on the Americans?
American based programmers seem to the be worst offenders. There are 2 main reasons for this:
  1. The USA is a large part of the English speaking market, too large to be ignored for developers outside it. Anyone wanting to sell software into there needs to take account of the US language and units. The reverse is not necessarily true.
  2. Observational bias. If someone was to hardcode metric and dd/mm/yyyy in their applications, I would never notice.
Update
I didn't put my location in the original post; my point is that you should write and program so that others can understand regardless of your, and their, location. Nonetheless, for those that are interested, I live and work in Hawkes Bay, New Zealand.

Friday, December 17, 2010

Developing on the Win phone 7 hardware without a developer account

Microsoft have released a free toolset for developing Windows Phone 7 applications. However you cannot debug or deploy onto actual hardware until you create a developer account @ $99 per year.

In my case, I only have a demo phone for 2 weeks so a developer account seems like a waste of money.

To work around this restriction, you need to do the following:

1 Unlock the phone:
Download and install the Chevron unlocker and certificate from here.

2 Stop the phone from relocking every time you reboot
Edit your hosts file (C:\Windows\System32\drivers\etc\hosts)
Add the line
127.0.0.1 developerservices.windowsphone.com

You can now deploy and debug on your phone.

Note: the phone will still relock itself every couple of weeks, giving the following error

“[application name] has been revoked by Microsoft. Please uninstall it.”

Just rerun Chevron and unlock again.

2nd note: You can deploy your .xap file to others and they can run them on unlocked phones. However the continual relocking may get annoying.

3rd note: You can't use Chevron to pirate applications. It will only allow you to run unsigned applications.