I’m currently working on porting the run-away hit mobile application ShopSavvy to Windows Phone 7 (sign up to be a beta tester) and one of the things I really wished I had was the WrapPanel.
(For those who are new to Silverlight/WPF/Windows Phone 7, the WrapPanel stacks UI elements either horizontally or vertically like the StackPanel except that, when it hits the panel limit it… wait for it… wraps and starts stacking in a new column/row. Handy.)
I found a way to add it with the Silverlight Toolkit, but doing that added 140 Kb to my application. Maybe I’m just stuck in the world of J2ME, but adding 140 Kb to my mobile app isn’t OK if I can help it. I found this post from Jeff Wilcox on adding the WrapPanel and that didn’t seem to work for me. (My comments are the whiny ones at the end of the post.)
But I finally got it working and I’ll tell you how.
All you have to do is download the DLL, unzip and reference in your app like so:
Right click on the “References” section of your project.
Select the WP7Panels DLL from whatever folder you put it in and see it show up in your References
Open up the “Assets” section and type “wrap” or “dock” the wrap-or-dock panel should show up. Double click it or drag it onto the canvas to add it to your app. Blend will take care of the rest of the namespaces and everything.
And start adding items to your panel. It should work the way it’s supposed to work.
One note with the DockPanel… items placed in the DockPanel need to have the attached property
[xmlns]:DockPanel.Dock=”[something]”
if you want them to do something other than align in the center. The [xmlns] name should be the same thing that is before the “DockPanel” in the XAML. It will most likely be “WP7Panels”. Therefore, the following XAML:
If you want to add the actual files to your Windows Phone 7 app instead of just referencing the DLL (perhaps you are crazy or you have a deep and desperate need to fully understand absolutely everything you touch or you are a C++ developer), you can download the source. I’ve consolidated the DockPanel code into a single file (DockPanel.cs), but you’ll need the following files to get the WrapPanel working (all included in the source).
LengthConverter.cs
NumericalExtensions.cs
OrientedSize.cs
TypeConverter.cs
WrapPanel.cs
Finally, if you need something else for Windows Phone 7 from the Silverlight Toolkit, you can either add the entire toolkit (explained here) or download the source for the latest Silverlight 3 build and open it in Visual Studio 2010 (for some reason, I couldn’t get it to open in Visual Studio 2008). It should update and you should be all set playing around in there to extract the stuff you need.
This is meant to be the one-stop-shop blog post for creating a very simple web service in PHP that pulls from a MySQL database and displaying the data in Silverlight. Emphasis on the “simple”.
Here is an example of the finished product (I reserve the right to clean up the data on a regular basis):
In Part 1, we created the MySQL tables necessary to store data for a simple to-do list. In Part 2, we’ll write some PHP code that will give us the ability to grab the data out of the database and send it, in JSON format, to our Silverlight application.
We’ve added the to-do table and column names so that, if we decide to change anything later, we can just go to this file and update the table or column once.
Just for good measure, we’ve added a function we’ll want to use across our php files. The function “formatInput” will be used to make sure all our data is decoded from the URL that calls our web service (the urldecode method) and then try to block any SQL injections (the mysql_real_escape_string method).
Now, let’s write the basic “Get the data” file. What we’re going to do is write it so that the we can choose to get:
all the to-do items
all the to-do items that are “done”
all the to-do items that are “not done”
This range of functionality isn’t even close to ideal. In a perfect world, we would want a wider range of options in gathering items (for example, items that contain a certain word or one item in particular or limit the number of items we call by a date range). However, for our very simple purposes, this will do.
The way our web service will work is that we have a URL that we’ll call from Silverlight when we want to get some data. When we calls this web service, we may want only the “not done” items or only the “done” items. We’ll handle that option by adding “?itemStatus=done” or “?itemStatus=notDone” to the end of the URL.
will get all the to do items that are complete. So we need to make sure that our web service responds appropriately to both calls.
There are comments in the code, but I’ll just explain the basic concept in picture form:
We take in a URL, extract the variables from it, create the MySQL query based on the variables, execute the MySQL query, extract the results, and then send back the php object encoded as a Json object. Each one of our files will follow this same pattern.
get_todo_items.php
<? include‘mysql_vars.php’;
// set up the “itemStatus” URL option and build a query addition
// to account for the itemStatus variable $itemStatus = $_GET['itemStatus'];
$itemQueryAddition = “”;
// … then encode the results as JSON Text…
// we’re using a “returnType” field so that our Silverlight application can differentiate between
// the kind of return values it recieves and parse the Json object appropriately
// … and print the results so that our app can read them echo $JSONResult; ?>
The other two files, add_todo_item.php and change_status.php use exactly the same structure to add a new item and change the status of an existing item (respectively). I won’t put all the code here in this post that is already too long, but you can download all the files here.
Update the mysql_vars.php file to fit your needs and you should be able to just upload these files and have your running to-do web service all ready for Silverlight to call it for data, which is something we’ll deal with in Part 3.
This is meant to be the one-stop-shop blog post for creating a very simple web service in PHP that pulls from a MySQL database and displaying the data in Silverlight. Emphasis on the “simple”. This tutorial is geared toward someone who has never done databases or web services.
Here is an example of the finished product (I reserve the right to clean up the data on a regular basis):
This tutorial will walk through the steps to create a simple to-do list. Our to-do list will hold text of what it is we need to do and a value indicating if the task has been done. In this tutorial, we will create a MySQL table to hold our data, a PHP service to call the data and a Silverlight application to display and interact with the data.
First, let’s create the table we need for our data.
If you don’t have or don’t like phpMyAdmin, the MySQL query to create the table described below is:
CREATE TABLE `[your_database_name]`.`to_do_data` (
`index_key` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`is_done` TINYINT( 4 ) NOT NULL DEFAULT ’0′,
`to_do_text` TEXT NOT NULL ,
FULLTEXT ( `to_do_text` )
) ENGINE = MYISAM
If you have phpMyAdmin, I highly suggest using it if you’re a MySQL novice. In phpMyAdmin, go to your database and enter the name of the table you want to create in the “Create new table on database [your_db_name]” section.
We’ll call our table “to_do_data” and give it 3 fields.
index_key – identifies the to-do item uniquely
is_done – true/false value indicates the status of the to-do item
to_do_text – a short text to describe what needs to be done
In phpMyAdmin, it will look like this:
A couple of notes about the fields:
index_key
This is the primary key of the table and is used to uniquely identify the given row. As such, it cannot be null and it auto-increments as rows are added to the table.
is_done
We use “TINYINT” type for this value because using BOOLEAN is basically the same thing. It is not null because every item must either be “done” or “not done”. “Done” = true = 1 and “not done” = false = 0. We are going to default to “0” (false) because we’re assuming that users aren’t going to make a to do list of stuff that is already complete.
to_do_text
We have artificially limited the text size to 1024 because we assume that this will be a set of short to-dos to, not a set of journal entries. We’ve also turned on “Fulltext” which lets the MySQL database index our entries for quick searching.
Now we just click the “Save” button at the bottom and we have our table.
Now our database system is in place and we’re ready to write a PHP webservice to implement all the CRUD (create, read, update, delete) capabilities our system will need.
If you want to learn more about MySQL, I highly recommend “A Visual Introduction to SQL“. This is the book I have, a left-over of my grad school years and it is an exceptional book for picking your way through various database scenarios.
If you’re looking for something a bit less costly, “MySQL Crash Course” is a fine book on the subject as well.
If you’re interested in government transparency, there is not better organization to watch than the Sunlight Foundation, a non-profit organization that is leading the charge to more government transparency through the release, organization and visualization of government data and processes.
Every once in a while, Sunlight Foundation runs contests to either visualize or interact with government data. I submitted a Silverlight project called “Recovery Review” for the latest contest, “Design for America”. With Recovery Review, users can search and visualize stimulus projects. If they find information that they think is inaccurate, they can flag that project for inaccurate information.
The idea behind the visualization is that users can search through the data using a couple of key points (how much money was awarded to a project, where the project is, how many jobs were reported as “saved or created” due to the project, etc) and the system will deliver in a visual format a set of results that match that search. It will also visualize (as seen above) the total of all awards that match that search in the context of the stimulus awarded as a whole.
If the user clicks on the on one of the projects in the list, the visualization resizes so the user can see how much money and how many jobs that individual project represents, both in relation to the search total and in relation to the total stimulus.
Here’s a video overview of the project (runs about 8 mins).
Looking at the competition, I find it unlikely that I will win, but building the project was a pretty exciting learning experience. I’ll be posting my lessons here in the next couple weeks.
In case you want to take a look at the unemployment visualization, I’ve added it below. I’m looking for a place to do ASP.Net hosting because my Bing Maps visualization is driven using Twitter data called and served through a WCF service, so if you have a recommendation for a noob ASP.Net guy, let me know. I’d love to host that as well.
(Apparently some people are having trouble with my demo below. I’ll try to get a more compatible version up soon.)
You can copy the code for to embed this with the text below:
AutoScroller For Silverlight 2 ScrollViewer – Working on my own scrolling issues… there seem to be alot of them, but the more I dig into the control, the more I understand why.
How to set the row height of a Grid in the code – Valuable if you're creating grids on the fly… which you shouldn't have to do, but sometimes the UserControls aren't working the way you want and the client deadline is approaching and you're beginning to panic and a some solution is better than no solution…
Yeah, yeah… I know it’s long past the time when anyone want to revisit the wonders of the 2008 presidential campaign, but the stars aligned for me to build this thing.
Star #1: I’m been meaning to learn JSON forever because everyone is talking about it.
Star #2: I’ve been meaning to get into pulling data from a publicly available API using a Silverlight application.
And so, due to several late nights and the infinite patience of my wonderful wife, I’ve finished my first data driven Silverlight information visualization. An explanation follows below the app.
The application visually displays how many stories in the New York Times in a given week mentioned one of the presidential candidates. If your favorite candidate does not appear, I apologize. I chose the seven most frequently mentioned candidates in each party.
My journey of exploration began at the New York Times developer site where I got my developer key and started exploring the data from their brand new Article Search API, with which you can search articles in the New York Times as far back as 1981.
Because the NYT API returns a JSON result, that meant I had to learn JSON. I highly recommend it. Super easy. I ended up scraping the data I needed and parsing it into a custom JSON file of my own so I wasn’t making a couple hundred calls to their servers every time I wanted to run my application.
From there, it was actually a simple matter to pull the idealized data into my application and start visualizing it. In case you’re wondering, the placement on the map doesn’t represent anything at all… I was just looking for a good way to show the data and I liked this one.
A special thanks to my wife, the math genius, for reminding me to take the square root my data so that my circles ended up with proportional areas (instead of exponential ones).
There are, of course, several things I would change about this if I had the time. The slider can be flaky, the fonts could use some more work and the code in general is a bit of a disaster. But this is all the time I had and I think it works pretty well.