Sunday, December 25, 2011

My first WP7 app, the shopping list

Beside the "big apps" like facebook, twitter or celsius, the application I use most on my iPhone is the shopping list. It's very helpfull not to write all the things on paper each time but just choose from a list of items I bought recently and add them with one finger movement. The list makes it very convenient to add my stuff and also remembers me of what to buy by looking at all the things from the last weeks. So this is defenitely the first app I need if I win a Nokia Lumia at the MSDN CH contest here.

There must be three elements in the app. The shopping list, the list with recent items and the posibility to add new stuff. A panorama app seems to be the perfect thing. With the panorama design I can quickly switch between the three screens.

A word and a blow. That's what the finished app looks like.




What else does the perfect shopping list need? It should tell you something about your list even if the app is closed. Cool that Windows Phone offers the live tile. This way I'm able to write the number of items to buy to the tile and on the back of the tile I write randomly changing the item names and quantity. So how does this look like? Here we go.



And what about the technical stuff? Was there anything exciting? Oh yes, the live tiles, the background agent task or building a drag&drop sortable list are very interessting tasks to do.

Everybody knows the sortable list from the iPhone. Seems to be a nice and simple control there, because it looks in every app the same and has the same functionality. I thought there must be a control like this in Mango. But there isn't. Thanx to Jason Ginchereau who gave as this missing control. Works very well and you can download it from his blog here.

Working with the tiles is the next tasks. Mango gives as a lot of power and working with the tiles is very easy. Writting the number of items to buy to the tile is as easy as this.

// Application Tile is always the first Tile, even if it is not pinned to Start.
var firstTile = ShellTile.ActiveTiles.First();

// Application Tile should always be found
if (firstTile != null)
{
// Set the properties to update for the Application Tile.
var newTileData = new StandardTileData
{
Count = number
};

// Update the Application Tile
firstTile.Update(newTileData);
}


Now let's take a look on how the background agents tasks write the items on the back of the tile. Read the details on how to use it here. First we need to register and start a periodic tasks. I start if the app is closing or deactivated.

// Obtain a reference to the period task, if one exists
var periodicTask = ScheduledActionService.Find(PeriodicTaskName.SHOPPING_LIST_AGENT) as PeriodicTask;

// If the task already exists and background agents are enabled for the application,
// you must remove the task and then add it again to update the schedule
if (periodicTask != null)
{
ScheduledActionService.Remove(PeriodicTaskName.SHOPPING_LIST_AGENT);
}
periodicTask = new PeriodicTask(PeriodicTaskName.SHOPPING_LIST_AGENT);

// The description is required for periodic agents. This is the string that the user
// will see in the background services settings page on the device.
periodicTask.Description = "This periodic task shows random items from your Shopping List in the tile background title.";

// Place the call to Add in a try block in case the user has disabled agents
try
{
ScheduledActionService.Add(periodicTask);

#if(DEBUG_AGENT)
ScheduledActionService.LaunchForTest(PeriodicTaskName.SHOPPING_LIST_AGENT, TimeSpan.FromSeconds(10));
#endif
}
catch (InvalidOperationException exception)
{
if (exception.Message.Contains("BNS Error: The action is disabled"))
{
ToastNotifications.Show("Background agents:", "Disabled by the user.");
}
}

And last but not least we need the agent who is doing the job. Implement this class in a seperate project and make sure to fullfill this requirements to pass the certification. In a very simple form this class looks like that.

public class ScheduledAgent : ScheduledTaskAgent
{

protected override void OnInvoke(ScheduledTask task)
{
#if DEBUG_AGENT
ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(10));
#endif
// Do work
TileNotifications.UpdateTile();

// Call NotifyComplete to let the system know the agent is done working.
NotifyComplete();
}
}


By adding a reference to this project Visual Studio also adds an entry to the WMAppManifest. That's it. Our shopping list app is finished.


And what else did I learned? If I build a WP7 app I need almost more time to do all the design stuff then programming. Nice icons, a cool background image and a well designed app is as important as the functionality itself. To make the user choose your app from marketplace where 100`000 of other apps are available, you need to have a good design.
Next thing that will help to sell your app is offering a trial version. To write good keywords in app hub is also a part of the success (and delimit them with a semicolon). An AboutPage is also a good plan. More about this in the next post.


And after all, if you think you will love the app like I do and it is helpfull for you (or your wife ;-) please get it here.


Wednesday, December 21, 2011

My first Windows Phone 7 Game

As I wrote in my last post my first windows phone app was a game called Simon aka Saimon or here in Switzerland better known as Senso. It's a classic brain game from the 80ies and everybody in my age will know the old version from MB I guess.

The goal of the game is to repeat the color sequences as long as you can. You will start with one color and with every right reputation one additional color will be added.

And this is what the game looks like.



To make the game more interesting I added different color schemas. You got the classic design in a nice Windows Phone Metro Style look, a dark and a bright color schema which are harder to play.
To make the game more addicting and more fun to play I added a statistic and you and your friends can set up a competition.



And now we take a look at the programming. Was there anything exciting? At the begining everything was straight forward. Take four buttons, setup some storyboards, ...
Then I wanted to add four sounds for the button clicks. In a Silverlight App for WP7 there is the media tag for a background sound. After a short google session it was clear not to use the media tag. It's not right thing for my needs, playing different sounds in a short sequence.
What I need is the SoundEffect class from the Microsoft.Xna.Framework.Audio. To make this sounds work you also need the FrameworkDispatcher class.
So first thing to do is to add a reference to the Microsoft.Xna.Framework.dll. Then you got access to all the classes you need. For the rest take a look at the Sound Sample.


After all, if you think you like the game get it here. Or if you not sure yet, wait for the trail version that is already submitted to Microsoft.

Sunday, December 18, 2011

How to start with Windows Phone Development

Before I actualy start let me tell you one thing: I can't realy write something new. Everything about windows phone development is already out there. But lets start the story three weeks ago.

Three weeks ago I wanted to do something new. At this time I read from @techpreacher on Twitter that Microsoft Switzerland started a contest where you can win a Nokia Lumia, the new Windows Phone from Nokia. Read more about it here.
That was good motivation for me and Windows Phone or short WP7 development was the thing to do. It was a good idea to get some more knowledge in this area specially because the Windows 8 Metro Style App developement is pretty close to WP7 development. This I learned at a MSUGS event where Maik Schärer talked about it. So this will also be a good investment for the future.

The idee for the first app was to build the cool Senso game from the 80ies which I already did ten years ago for the hp and compaq pda's everybody in my age will surely remeber. So let's start.

I already had a WindowsLive account, Visual Studio 2010 installed and some Silverlight knowlage. I asked Google what to do next. I need the Windows Phone SDK 7.1. Version 7.1 to build Windows Phone 7.5 (Mango) apps? Yes that's right :-) Get it from here. If you installed the SDK you got everything you need to develop a WP7 app and test it on the emulator. You don't evan need a windows phone.
Another must have tool ist the Silverlight for Windows Phone Toolkit which provides a lot of useful additional controls.
 
Now the technical part begins. Start Visual Studio, choose the right templete for your app, etc. It makes no sense if I write anything about the dev part in this blog. Everything on how to start you will find here, start under education. Another great resource is MSDN. And already in the first view hours I had a lot of specific questions, problems and no answers. All the answers I found on the great WindowsPhoneGeek site. Give it a try!
 
If you wanna make your App available to others, you need an account on AppHub. The registration is straight forward but will cost you a few bucks. The only strange thing was the exchange rate. $ 99.- in the US and CHF 129.- in Switzerland. C'mon Microsoft, what's wrong with you guys!? Anyway, pay and they do a real good job for you by selling your App to the whole world. I think it's a faire deal.
 
To get your App certified you have to fullfill a lot of requirements. It's a good idea to read this section on MSDN. It gives you a good understanding on what are the dos and don'ts so that your App will be available on the whole world soon.
 
Last thing I learned, give them a trial version of your App. The statistic sais, you will sell 70% more this way.
 
So now have fun developing wp7 apps and games!
 
Ohh, one last thing, buy my Apps on the Microsoft Marketplace and follow me on twitter for more great hints ;-)