I thank everyone who have used my Android template. It still works and you can still customize it to add new libraries. However, those who have migrated to latest versions of Visual Studio, a better alternative to this template is already available from Microsoft now.

I love Visual Studio. I love their express editions for being FREE. I love it because it gives superb intellisense as well as debugging support. The reason I enjoy coding .NET is probably because it's done in Visual Studio. I don't mind coding in any programming language as long as it was available in Visual Studio.

I am a huge fan of Android platform, and I wanted to develop some Android Apps, but not in Eclipse. I learned that PhoneGap can be used to convert HTML5 applications into Android APKs, so I thought why not give it a try! And of course what could be a better HTML5 development IDE than Visual Studio Express 2012 for Web, which I love most! Its JavaScript code-complete and debugging facility are second to none!

During this whole exercise, I had to perform a set of manual operations, so I thought of converting it into a reusable project template. Every time I want to create a new Android App, I just wanna have to click on 'New Project" and then "AndroidApp" and it should just work! And here it is! . I am sharing this template with all of you so that you can also share the joy of coding Android apps in Visual Studio.

Prerequisites:

You will still need the Java SE SDK and Android SDK because that is what's used underneath. Moreover, you will need ANT SDK as well in order to build. (CTRL+SHIFT+B in Visual Studio still works! It just calls the ANT SDK which performs the magic behind the scene).

1. Install Java SDK 2. Install Android SDK 3. Install ANT 4. Set Environment Variables JAVA_HOME, ANDROID_SDK_HOME and ANT_HOME to the path of respective folder where these SDKs are installed. 5. Add the following to the environment variable named PATH %JAVA_HOME%\bin;%ANDROID_SDK_HOME%\tools;%ANT_HOME%\bin;

6. Install Visual Studio 2012 Express for Web (or any edition as a matter of fact) 7. Extract the Bundle. It will have a readme file and a template file. Copy the AndroidAppVSTemplate.zip to My Documents\Visual Studio 2012\Templates\ProjectTemplates

8. Launch Visual Studio 2012 Express for Web and create a New Project. If you select 'Visual C#' from the tree, you will be able to see a new project type 'AndroidApp'. *** IMPORTANT *** Don't use space in the name of the app or even in the whole path, otherwise the build will fail.

If you want to build your app, just press CTRL+SHIFT+B. In case there was an error due to some missing environment variables, the build will fail. You can see the detailed error log in 'Output' window. If the build succeeds, an APK will be generated in the bin folder.

The app.html contains some sample HTML5+jQueryMobile code. The end result would look similar to the screenshot below

Note:

You can test your apk in emulator by running util\run.bat. If it does not work somehow, you can still resort to old fashioned way of using ADB.exe from android platform-tools.

There are a couple other HTML/JS/CSS files from the cordova package which I kept as it is. I just changed their name to something more meaningful. You can delete them if you want to.

The main entry point of application is index.html, which can be changed by modifying res\xml\config.xml. The following entry needs to be changed. <content src="index.html"/>

Please note that the template was created with Android SDK 12 as base. However, it is flexible and with just a little modification you can run in on any android version. The files you need to modify are 1. AndroidManifest.xml - Change the SDK number from 12 to the number of SDK you are using. <uses-sdk android:minSdkVersion="12" android:targetSdkVersion="12" /> 2. project.properties - Change the value of variable 'target' from 'android-12' to the number of SDK you are using.

Final Thoughts:

By the way, I am just a mediator, someone who thought of putting all pieces together in a single Visual Studio template. The real credit goes to folks who created wonderful frameworks such as PhoneGap, jQuery Mobile, knockout.js. The guys who thought of making HTML5 supercool also deserves their share of credit.

I hope you will like this template and be more motivated to create android apps, especially if you are not familiar with java or you are a Visual Studio lover like me. If you do like this, then don’t forget to drop a comment!

Win32 API offers a lot of functionality for Windows platform, which the .NET Framework doesn’t have for good reason – One of them being ‘managed’. However, that doesn’t stop us from doing some unsafe things! We can always build a wrapper for Win32 API and then call those functions directly from .NET.

Let us evaluate one of such area which requires calling Win32 API functions from .NET. That area is ‘Simulating UI Automation’. Of course, the subject is too vast to fit in a single blog post, however, we can start with some basic things at least!

Our objective for this exercise is : “Launch an instance of Notepad and write ‘hello’ in it”; (Sorry for the semicolon, it has become a habit )

So let us start with creating a C# Console application. Next step is to create a new class called Win32 (you can name it whatever you like) and declare a couple of ‘static extern’ methods whose signature should match that of the unmanaged native Win32 API methods. There are a couple of DLLs for core Win32 APIs. You can categorize them into 8 areas. For more info, please visit http://en.wikipedia.org/wiki/Windows_API. We will be focusing on User32.dll for today’s topic as it deals with the ‘User Interface’.

using System.Diagnostics;
using System.Threading;
using System.Windows.Input;

You will also need to add reference to WindowsBase.dll

Now, what we are doing here is utilizing the Process and ProcessStartInfo to start an instance of Notepad. Then after waiting for 2 seconds, we are making call to your UIAutomationHelper method called FindChildWindow with parameters as className = null, windowName = “Untitled – Notepad”, childClassName=”edit” and childWindowName=null. Now this method internally calls the Win32.FindWindow and Win32.FindWindowEx and returns a handle of the Notepad instance we want use. The PressKeys method of our helper is also making call to Win32.PostMessage method internally which is used to post key strokes to the Notepad window.

Now, let’s compile and run this program. This is what we get as an output.

So, this is how we go about writing automated text in the any window. Now, if you want to know more about what we can do with our target window (in our case it was notepad), you can use a utility called Inspect.exe which can be found at the following location.

C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\Inspect.exe

This utility is part of Windows SDK that can be downloaded from the following location.

ASP.NET has a good number of default controls which usually satisfy your needs. Especially the ListView control, which is a template based fully customizable control which can be tweaked to suit your needs. However, ASP.NET does not have a good Image Grid control (a kind of control you would want to use as a photo gallery), I will show you how you can create one simple Image Grid for yourself. I am using Visual Studio 2012 Express for Web for this exercise, however you can achieve the same with full version of Visual Studio 2012 or even with Visual Studio 2010.

The following is something we want to achieve

Step – 1 : Initial Setup

First create an empty ASP.NET Web Application. Now create one folder ‘Controls’. You don’t need to put your control in ‘Controls’ folder, however it is a good practice to put relevant things in its own folder.

Here you can see I have also create some other folders such as ‘Helpers’, ‘Images’ and Default.aspx (Remember? We chose empty ASP.NET web app, so Default. aspx will not be available by default).

Now, right click the folder and ‘Add’ –> ‘New Item’ –> ASP.NET Web User Control with name ‘ImageGrid.ascx’

Step-2 : Logger

As a habit, I always log exceptions so that later on the developer (I) will be able to determine if any error/exception occurred in production. For that purpose, I am going to write a simple logger which logs messages to Windows Event Log. You can write your own custom logger, or you may already have some good logging strategy in place (for example – ELMAH). If that is the case, then skip this step. Make sure you comment out Logger.Log(ex.Message) statements in the next sections, which uses the Logger class I am creating here.

This ListView is not bound to any DataSource yet, as we are going to bind it to a List<string> providing image path programmatically.

The most important piece of code lies in ItemTemplate and InsertItemTemplate. Inside ItemTemplate, we have 2 buttons. One ImageButton to display the image and a LinkButton to Delete the Image.The ImageButton binds to the current iterated item of the DataSource using Container.DataItem as ImageUrl. The same url is also used as CommandArgument which will be passed to the EventArgs while firing OnCommand, which is handled by itemImageButton_Command in order to show the Full Image.

The LinkButton is not Visible by default. It will be visible only when the AdminMode is enabled (We’ll soon cover how to set AdminMode). This check is handled in the Load event of the LinkButton.

Similarly the InsertItemTemplate is also visible only when the AdminMode is enabled.

In other words, If AdminMode is set then you can upload new Image as well as delete existing Images. If it is not set then all you see is the 320x240 icons of the Images in the ImageFolderPath (We’ll also cover how to set the ImageFolderPath in a moment). All can do is click on the image icon and it will display the FullImage.

Step-4 : Image Grid Code-Behind

Now it is time to write the Code-Behind. Instead of pasting the full code-behind, let us take it in steps so that we can better understand it piece-by-piece.

We have 2 private members to store the virtual and actual path of the Image Folder, which can be set via the public property ImageFolderPath. Any public property of a User Control (ascx) can be set via attributes.

For Example, just as you would set <asp:TextBox Text=”something ….. you would use <cc:MyCustomControlOrWhatever MyProperty=”” ……. So when you will actually use this grid in an ASPX page, it will be very simple to set any folder containing images in ImageFolderPath attribute and whether you want to display the AdminMode controls (upload and delete functionality). If you want to control AdminMode programmatically by identifying whether the user has access or not, it is just as easy, because ultimately it is just a public property of the ImageGrid class.

When the ImageGrid loads, it will first update the virtual and physical path by using the values inside ImageFolderPath. If AdminMode is enabled, it will enable the InsertItemTemplate. Before actual rendering, the data binding will happen and the ImageGrid will be updated with current Images. UpdatePath() and BindData() are shown in the snippet below.

The above snippet is self-explanatory. Just note one thing that the code takes the default path as “~/Images”. In other words, if you don’t specify ImageFolderPath, then by default the code will pick up Images inside ‘Images’ folder. Another good practice would be to put all the images in such a separate folder.

The delete operation is performed by the above snippet. Here, instead of ‘Delete’ we have passed a custom ‘Remove’ command, which seems odd. This is done order to utilize the CommandArgument which is not available had we used the ‘Delete’ command.

Before using this last piece, however, make sure you have setup Roles and the current user accessing the page is in ‘Administrator’ role. Of course, Membership and Roles are totally different topics and out of the scope of this tutorial.

Download the Source Code

You can download the source code from the following location:

Well, I hope you have enjoyed this tutorial. If so, please do comment.

Could not load file or assembly 'System.Data.SQLite' or one of its dependencies. An attempt was made to load a program with an incorrect format.

Likely to be reproduced when:

You will usually encounter this problem when you have downloaded a sample application that is a 32-bit application targeted for ASP.NET 2.0 or 3.5, and you have IIS7 on a 64-bit OS running .NET 4.0, because the default setting for running 32-bit application on IIS7 with 64-bit OS is false.

When you compile your C# code, it will get converted into a managed module. A managed module is a standard EXE or DLL. This managed module will have the IL (Microsoft Intermediate Language) code and the metadata. Apart from this it will also have header information.

The following table describes parts of a managed module.

Part

Description

PE Header

PE32 Header for 32-bit PE32+ Header for 64-bit

This is a standard Windows PE header which indicates the type of the file, i.e. whether it is an EXE or a DLL. It also contains the timestamp of the file creation date and time. It also contains some other fields which might be needed for an unmanaged PE (Portable Executable), but not important for a managed one. For managed PE, the next header i.e. CLR header is more important

CLR Header

Contains the version of the CLR required, some flags, token of the entry point method (Main), size and location of the metadata, resources, strong name, etc.

Metadata

There can be many metadata tables. They can be categorized into 2 major categories. 1. Tables that describe the types and members defined in your code 2. Tables that describe the types and members referenced by your code Apart from that, there is another category of tables called Manifest tables, which can be found in the keeper PE file (An assemble could consist of more than one PE files)

IL Code

MSIL representation of the C# code. At runtime, the CLR converts it into native instructions