Archive

Author Archive

Debugging External Library In Visual Studio

January 27, 2013 Leave a comment

I was searching on how to debug an external library in Visual Studio, then found this:

http://stackoverflow.com/questions/654696/how-to-debug-external-class-library-projects-in-visual-studio

I just blogged it so I can easily find this again in the future. One thing to note is that you should have the library’s PDB file, which should match the build.

Multiple WebAPI Post Methods

January 22, 2013 Leave a comment

I was working on a WebAPI project and realized that I needed multiple post methods. I have an existing method like this one:

public HttpResponseMessage PostProduct(Product product)
{
}

And I needed another post method that will accept an array. It can be done by using the ActionName attribute.

[ActionName("CreateProduct")]
public HttpResponseMessage PostProduct(Product product)
{
}

[ActionName("CreateProducts")]
public HttpResponseMessage PostProduct(Product[] products)
{
}

That should do it. The URLs to these methods will also change, say from /api/Products to /api/Products/CreateProduct. This will break existing clients. If you want to retain the URL used by the first method, then you can add a new route like this:

routes.MapHttpRoute(
    name: "ProductPostRequest",
    routeTemplate: "{controller}/{action}",
    defaults: new { action = "CreateProduct" },
    constraints: new { controller = "Products", httpMethod = new HttpMethodConstraint("POST") }
);

This just means that the CreateProduct will be the default method for Post requests on the Products controller.

Categories: WebAPI Tags: ,

Inheriting WebClient in Windows Phone

May 26, 2012 Leave a comment

I was working on my Windows Phone app when I encountered a runtime error:

Inheritance security rules violated by type: ‘<class name>’. Derived types must either  match the security accessibility of the base type or be less accessible.  If the base class has a non-transparent default constructor, the derived class must also have a default constructor, and the method inheritance rules apply across those two methods.

It was caused by inheriting the WebClient class. Fortunately, the fix was easy and we just need to specify an attribute to the derived class’ constructor.

public class DerivedWebClient : WebClient
{
    [System.Security.SecuritySafeCritical]
    public DerivedWebClient()
    {
    }
}
Categories: Windows Phone

9GAG Metro Now Published in Windows Phone Marketplace

March 10, 2012 7 comments

My first windows phone app, 9GAG Metro was recently approved and published in the Windows Phone Marketplace. Here’s the link: http://www.windowsphone.com/en-US/apps/927f98ac-ba6b-41ee-8673-eea59e7815f8 . Although there are already a lot of 9GAG client apps in the marketplace, most of them lack features that I want. That is why I decided to create an app. I’m not saying that my app currently has a lot of features. Actually, it is the opposite. But, I can work on new features during my spare time and continuously improve my app. I do have an update already submitted and is waiting for the certification results.

So what is the difference of this app compared to others? The main difference is the image quality of long posts. To ensure that Windows Phone apps perform well, one of the requirements Microsoft set is to have images displayed to at most 2000 by 2000 pixels. Other apps display long posts by scaling them to fit within the 2000 pixels requirement. Unfortunately, some long posts containing text tend to be unreadable after scaling. 9GAG Metro addresses this problem by breaking down the image into smaller images. The trade-off is that it consumes more memory.

Another feature I included is the ability to read comments. Sometimes, there are some posts that you don’t understand. Commenters usually explain the post so I decided it is quite necessary to be able to read comments. Unfortunately, this feature is currently broken and causes the app to crash. I will post a detailed blog post on what caused this. The fix is included in the new update I submitted.

It failed certification once, which I wrote about in a recent blog post: http://mdetras.com/2012/02/25/my-first-submission-to-the-windows-phone-marketplace/ .

Update: Version 1.1 was already published to the marketplace. This includes bug fix for viewing comments, image zoom, and viewing recent posts. Please check http://mdetras.com/9gagmetro/ for details and how-tos.

My First Submission to the Windows Phone Marketplace

February 25, 2012 1 comment

My first app failed testing some time this week. It is a client app for the 9GAG web site. If you are familiar with 9GAG, then you know it is full of adult jokes. Unfortunately, you can’t target a few countries for distribution of your app if it contains these kinds of jokes. And I set my app to be distributed worldwide. I have read the requirements before I submitted but I tried it anyway. Talk about being stubborn.

I also want to share some details of the test results. I got it in PDF format, which was forwarded to me by YallaApps, a global publisher (I am from the Philippines, and it is not yet listed in the App Hub). My submission was received on February 17, 2012, and the testing was completed on February 22, 2012. So, it was completed in 4 working days. The app was tested on 4 different devices: HTC HD7, LG Quantum, Nokia Lumia 710, and Nokia Lumia 800.

The test results also contain the violation/s (I’m not sure if they lists all the violations). My app only got one:

“Content that is offensive in any country/region to which your application is targeted not allowed. Content may be considered offensive in certain countries/regions because of local laws or cultural norms. Examples of potentially offensive content in certain countries/regions include, but are not limited to the following:

  • People in revealing clothing or in sexually suggestive poses
  • Religious references
  • Alcohol references
  • Sexual or bathroom humor
  • Simulated or actual gambling

Countries/regions for which one or more parts of 3.10 may apply include the following:

  • Group 1: China
  • Group 2: Malaysia, Indonesia”

In the comments section, the testers added the steps to reproduce.

  1. Launch application
  2. Navigate between articles using “<-” icon (newer)/”->” (older)
  3. Observe that the application contains adult jokes and includes countries listed in Group 2 within Application Certification Requirement 3.10 in its targeted distribution.

That’s it. I resubmitted my app and selected the appropriate countries in the target distribution. Unfortunately, YallaApps has some countries missing, including the Philippines (which was newly added to the marketplace). I still have yet to contact them regarding this matter.

Displaying Large Images in Windows Phone

February 3, 2012 Leave a comment

I was working on a windows phone app and I needed to show a large image downloaded from the web. I found out that an image should have a max dimension of 2000 x 2000 pixels in windows phone due to performance requirements. I thought of splitting the image into smaller images that will fit the max dimension. To be able to split the image, we need to get its dimensions.

If you used the Image control and set the Source property to the URI of the image, you won’t be able to obtain the original pixel width and height (at least that’s what I know). The BitmapImage source of the Image control might already be downsampled or cropped, so the dimensions won’t be the same as the original. An easy workaround is to use the WebClient class to download the image.

The following is an example code that uses downloads the image using WebClient, splits it into images with a height of 1000 pixels or less, and set them as the ItemsSource of a ListBox with the DataTemplate set to <Image Source=”{Binding}”/>. I used the WriteableBitmapEx library to crop the image. You can download it via NuGet, or the codeplex site http://writeablebitmapex.codeplex.com/.

public partial class MainPage : PhoneApplicationPage
{
    private WebClient webClient;
    private List bitmaps;

    // Constructor
    public MainPage()
    {
        InitializeComponent();

        bitmaps = new List();

        webClient = new WebClient();
        webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
        webClient.OpenReadAsync(new Uri("http://www.someurl.com/someimage.jpg"));
    }

    private void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        var rawBitmap = PictureDecoder.DecodeJpeg(e.Result);
        var xOffset = 0;
        var yOffset = 0;
        var rawBitmapHeight = rawBitmap.PixelHeight;
        var rawBitmapWidth = rawBitmap.PixelWidth;
        while (yOffset < rawBitmapHeight)
        {
            var croppedBitmap = rawBitmap.Crop(xOffset, yOffset, rawBitmapWidth, 1000);
            bitmaps.Add(croppedBitmap);
            yOffset += 1000;
        }
        ImagesListBox.ItemsSource = bitmaps;
    }
}
Categories: Windows Phone Tags: ,

Unable to load DLL ‘SQLite.Interop.DLL’

September 29, 2011 1 comment

I am working on a ASP .NET MVC 3 project and decided to use SQLite. I found a great Code Project article titled SQLite Membership, Role, and Profile Providers by Roger Martin. He made SQLite providers similar to those for SQL Server. I decided to follow the article step-by-step. After build, I opened the Security tab of the ASP .NET Web Site Administration Tool page (via the ASP .NET Configuration item under the Project menu), and got the following error message:

Unable to load DLL ‘SQLite.Interop.DLL’: The specified module could not be found. (Exception from HRESULT: 0x8007007E)

It seems that the System.Data.SQLite DLL needs the SQLite.Interop DLL in the same directory. So we’ll just need to copy that DLL to our bin directory. Of course, we would want to have Visual Studio do it automatically for us when we build our project. If you have added the DLL to your project you can set the “Copy to Output Directory” property of the said DLL. That is, it should not be inside a subfolder of the project. Otherwise, the DLL will be inside a subfolder in the output directory.

If the DLL is in a sub-folder we can still use Build Events. In the project properties, go to Build Events and put the following command in either the Pre-build or Post-build event command line (assuming that you have put the SQLite.Interop DLL in a lib subfolder):

Copy “$(ProjectDir)lib\SQLite.Interop.dll” “$(ProjectDir)$(OutDir)SQLite.Interop.dll”

Categories: SQLite Tags:

Use Google Maps in Android Using Eclipse and Debug Key

July 10, 2011 1 comment

Google Maps API is not included by default in the Android SDK. Download it first using the Android SDK and AVD Manager. It is included in the Google APIs by Google Inc. under Third party Add-ons.

To use the Google Maps service, we need to obtain a Google Maps API Key. We need to sign our android application and get the MD5 fingerprint of the certificate. In this example though, we’ll just be using the Android SDK’s debug certificate. This is the one used by the Eclipse ADT plugin to run our application in the emulator. Detailed steps in obtaining a key can be found here: http://code.google.com/android/add-ons/google-apis/mapkey.html. To make things short, go to the .android folder and execute the following in the command line: keytool -list -alias androiddebugkey -keystore debug.keystore -storepass android -keypass android. Here is a sample screenshot.

Copy the generated MD5 fingerprint and go to the following web page: http://code.google.com/android/maps-api-signup.html. Assuming you already have a Google account, enter the MD5 fingerprint in the specified text box and click the Generate API Key button. This will show you to another page containing your API Key.

When you create your project, ensure that the build target is set to Google APIs. If you already have an existing project, go to the project’s Properties window > Android and change the Project Build Target.

After that, open the AndroidManifest.xml file and add these two lines:

<uses-permission android:name=”android.permission.INTERNET” />
<uses-library android:name=”com.google.android.maps” />

Here’s a sample:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mike.mapexample"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
    	<uses-library android:name="com.google.android.maps" />
        <activity android:name=".MapExampleActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

To display the map on the main screen of our app, open the main.xml under the res > layout directory and add a MapView like below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
	<com.google.android.maps.MapView
	   	android:layout_width="fill_parent"
	   	android:layout_height="fill_parent"
	   	android:apiKey="<your API Key here>"
	   	/>
</LinearLayout>

Next, our activity must inherit from MapActivity instead of Activity. Import com.google.android.maps.MapActivity and implement the necessary methods.

package com.mike.mapexample;

import com.google.android.maps.MapActivity;
import android.os.Bundle;

public class MapExampleActivity extends MapActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
}

That’s it! You should now be able to see Google Maps in the emulator, assuming you have run the appropriate virtual device. If you still haven’t, go again to the Android SDK and AVD Manager. Go to Virtual devices and create a new android virtual device. Select the Google APIs (Google Inc.) with the desired API Level.

Set this device as the emulator for your project. Here’s a screenshot of the emulator running Google Maps.

Categories: Android Tags: ,

Open Existing Android Project in Eclipse

June 26, 2011 Leave a comment

I downloaded sample android applications from http://code.google.com/p/apps-for-android/. Coming from a .NET development background and using Visual Studio as IDE, it took me several minutes to figure out to open a sample app in Eclipse. To open an existing project, click on File > Import… menu item.

Select “Existing Projects into Workspace” and click the Next button. Select the root directory where the samples are located. Eclipse will search for existing projects in the said folder. Although there are a lot of samples in the folder, I think Eclipse will only load samples that have .project and .classpath files.

Afterwards, select the project that you want to import. You can optionally copy the project to your workspace. Click the Finish button and you should already see the imported projects in the package explorer. In my case, I get the following error:

[2011-06-26 11:20:12 - WebViewDemo] /WebViewDemo/gen already exists but is not a source folder. Convert to a source folder or rename it.

Just delete the gen directory in the package explorer and Eclipse will automatically regenerate it. That will clear the error.

Categories: Android Tags: ,

Getting Started with Android Development Using Eclipse

June 19, 2011 2 comments

I wanted to learn programming in Android, and might as well create an app that could generate extra income. I wanted to blog my experience with Android development and I hope that someone else find my posts helpful. Anyway, before I could create an Android program, I’ll need to install the tools to get started. I’ll be following the instructions laid out in http://developer.android.com/sdk/eclipse-adt.html. It’s been a long time since I used Eclipse because I use Visual Studio at work. I think it’s not required to use Eclipse for Android development but having an IDE would certainly make life easier.

First, we need to download and install the Android SDK. You can download it in http://developer.android.com/sdk/index.html. Select the appropriate version for your operating system. The installer will first check if you have a Java Development Kit (JDK) installed. If it is not yet installed, it won’t continue so just download the JDK and install it first. Afterwards, run the SDK manager to download packages. In my case, I just downloaded the packages checked by default.

You can download the Eclipse IDE in http://www.eclipse.org/downloads/. I downloaded the Eclipse Classic version. It is a zipped file containing the binaries. Just unzip the file and put the contents in the directory you desire. There is no setup required. You may also want to create a shortcut on the desktop. Open up Eclipse and select the directory for your workspace. Go to the Help > Install New Software menu. It opens up the Install dialog. Click on the Add button to add the ADT Plugin repository like shown below.

Afterwards, select the ADT Plugin in the “Work with” combo box. Also select the items that show up on the list.

Continue with the wizard, accept the license agreements, and install the selected software. Restart the Eclipse IDE after. Go to Window > Preferences. Select Android from the list. The Android SDK location is not set yet. Set it to the Android SDK directory. On my machine, I set it to C:\Program Files\Android\android-sdk. Click on the Apply button and the list of SDK targets is refreshed.

To test if our setup is working perfectly, we can try running a sample included in the Android SDK. Check the C:\Program Files\Android\android-sdk\samples directory for the different samples. There are different folders for each API level. To use one, click on the File > New > Other… from the menu. It will show a dialog with different wizard choices. Choose the Android Project wizard. It will open the New Android Project dialog. Select the “Create project from existing sample” radio button. Select the build target and the sample you want to run. I chose the 2.3.3 build target in this example.

Click the Finish button. You can see the added project in the Package Explorer. Right-click on the project and select Run As > Android Application. You can also run the application by using the Run button in the toolbar. You may be prompted to add a new Android Virtual Device. Click Yes and the Android SDK and AVD Manager dialog will be shown. You can see the list of available virtual devices. Click on the New button to create a virtual device compatible with the desired build target.

Click on the Create AVD (Android Virtual Device) button. Run the application again and it should launch the emulator.

Categories: Android Tags: ,
Follow

Get every new post delivered to your Inbox.