As part of developing for WP7, I came across a few issues that I’d like to share with you, and maybe help you avoid some pitfalls I faced…

1. WebClient requests are cached by emulator
If you use the WebClient class to download information from the web, be aware that the web requests are cached by the emulator (and the actual phone?) and this can be quite confusing, especially for real-time data from the web. There is no straight-forward workaround, and the only way for me to get this working was to tag on a GUID in my query string. Something like:

downloadUrl = url + “&guid=” + Guid.NewGuid()

This, of course is a hack which might work with most websites because they only pick out params they care about and ignore the rest. Watch out for some strict sites that check for a fixed number of params and I have nothing around that at the moment.

2. Fiddler with WP7

As part of troubleshooting my first problem, I tried to use Fiddler which will not track WP7 web requests out of the box, but can be made to do so with the following steps:

  1. Start Fiddler (ensure you have the latest version).
  2. Click Tools > Fiddler Options.
  3. Open the Connections tab and tick the Allow remote computers to connect box. Click OK.
  4. In the QuickExec box (shown in red) under the session list, type prefs set fiddler.network.proxy.registrationhostname [HostName] where HostName is the name of your desktop computer.

image

   5. Close and restart Fiddler and the Windows Phone 7 Emulator.

Now you should  be able to track requests from the Emulator in Fiddler.

I referred to Eric’s blog post and the Phone 7 blog for this issue.

3. Application Icon and Splash Screen don’t show up in app
I replaced my ApplicationIcon.png and SplashScreenImage.jpg but was scratching my head when they did not show up at run-time. A little looking into showed that when I replaced the original files, I lost their Properties and these files need to marked as Content Resources, like this:

image

4. WP7 toolkit installation problems

I had a few headaches getting the toolkit to install, and it kept failing for some reason and I think they were related to not having VS Express (or whatever the free version is).
I don’t remember what I did exactly, but there are people who have workarounds and a few retries worked for me, for installing the XMA framework for example.

If you’re having an installation problem, look for the toolkit iso and shut down and even reboot and try again. If not, ask in the WP7 forum at create.msdn.com and hopefully you will get some help.

5. Consider the LongListSelector control for nested lists

I had a situation whereby I had a kind-of master-detail view shown together where the master items are shown with their details items next to them, for eg.

Master 1
      detail 1.1
      detail 1.2

Master 2
     detail 2.1

Master 3
    detail 3.1
    detail 3.2
    detail 3.3
    detail 3.4

I started with nested Listboxes but then realized that the ItemsControl is also a possibility since I did not need Listbox behavior like selection of items, etc.
Think of ItemsControl as the insides of a ListBox.

But then scrolling became an issue because I wanted all items to be visible at once and not need to scroll the detail list for item 3 for example.

Fyi, you can make the ItemsControl scrollable by adding a View Template, like so:

<ItemsControl x:Name="myItems">
 <ItemsControl.Template>
  <ControlTemplate TargetType="ItemsControl">
    <StackPanel>
      <ScrollViewer VerticalScrollBarVisibility="Auto" Margin="0,0,0,0">
	<ItemsPresenter />
      </ScrollViewer>
    </StackPanel>
  </ControlTemplate>
 </ItemsControl.Template>
</ItemsControl>
There are many nice posts about ItemsControl which you can look at to tweak and use them well.
Unfortunately, they couldn’t work easily for me, since I needed to autosize the nested details so that either 1 or 4 items could be shown without scrolling.
I say easily because I am sure sure some WPF gurus can do almost anything with ListBoxes, but not me.
I am now working to adapt the LongListSelector control from the toolkit. It was mainly made for virtualizing the View and the data to give you a fast scrolling and grouped list.
The Contacts list on the phone is a good example of it.
So, if you want to use nested lists and want the children to autosize, etc., look at the LongListSelector sample in the toolkit.
This mini-series by WindowsPhoneGeek helped me understand it better.

6. Re-entrancy problems are possible with MVVM Light
I am using the MVVM Light framework which is quite handy for locating ViewModels, but you also need to be careful about manipulating collections while in a loop.
This is more an issue of bad implementation than the framework itself, but iterating over static ViewModel is enticing and a common mistake and should be done with caution.
If you change any item in the List will throw an Exception. I forget now exactly, but I think I was getting a NullReferenceException.
So, my advice while manipulating a ViewModel List, is to create a new List and iterate over the static ViewModel List and copy it’s contents and then set the ViewModel list to your new local List.
Hope these points saved some of your time…

Advertisements

Since everyone (including me) and their mother is now dabbling in WP7 development, I thought I’d write about some of it and share my experiences with the community.

Before getting into dev details, let’s start with the setup and getting off the ground.

The page to access the wormhole is at: http://create.msdn.com.
It has a link to download the (free) tools you’ll need.

image

Please go ahead and install the various components required; I’ll wait.
The pieces include the VS extensions which installs the various phone app templates, the WP7 emulator that lets you run and test apps without a real phone in hand, etc.

We are now ready to write our first app.
Note that we are going to explore Silverlight apps and not XNA apps. These run on a managed Silverlight layer on the phone, as opposed to an XNA platform that conceptually treats the device like a console where you can manipulate each pixel.

Open VS and select New Project –> Silverlight for Windows Phone – Windows Phone Application (the first item), as shown below.

image

Give it a name and location and you will be faced with the Mainpage.xaml

image

If you just want to run the project and hit F5, you will get a compile error stating that “Zune software is not installed. Install the latest version of Zune software.”

image

This happens because the output is set to a device, and you can change it to go to the Emulator by changing the dropdown selection next to the Play button to Windows Phone Emulator.

image

Hitting F5 now, loads the Emulator and shows the app in it.

To end debugging, the instinctual way is to close the Emulator. But I like to keep the Emulator running in order to save time in further debugging sessions.
Hit the Back <- button at the bottom of the Emulator or go to VS and hit Stop to exit from Play mode.

So, now you have a very basic Phone app running in Visual Studio. That’s all for now. More to come in future posts…

If you have sets of data that have a visual element, and would like to show them as sets that a user can interact with, e.g. zoom-in, filter, go to linked content, etc., consider a tool called Pivot from Microsoft Labs.

You can get it and read all about it at: www.getpivot.com
A good presentation with some design principles is at: http://live.visitmix.com/MIX10/Sessions/FTL02
An older TED presentation about the technology, that shows interesting possibilities: http://www.ted.com/talks/gary_flake_is_pivot_a_turning_point_for_web_exploration.html

Application showing a sample image set:

Note: The grouping, filters, etc. are automatically generated by the tool, based on the XML used to define the data-set.

I shall write more about this technology after I play with the Silverlight Pivot control.

I have a creaky old Win 2003 Server dev box that I wanted to deploy my RIA solution to. I could not do a build and publish on the box itself because I did not want to install the RIA services, etc. on there.

So I did a publish on my local environment, which produced a folder with the required aspx files and a bin folder with all the RIA Dlls (include data annotations and domain services) and a ClientBin folder for the Silverlight XAP client payload.

I copied the folder as-is to my web server and created a virtual folder and an Application (in IIS) for it.

But when I browsed to it, I got an error in my browser stating:
Message: Unhandled Error in Silverlight Application
Code: 2104   
Category: InitializeError      
Message: Could not download the Silverlight application. Check web server settings    

This is slightly misleading since you might think there is something wrong with your application, but in fact it happens because the web server is not serving the required payload properly. It is because your web server may not have the required MIME types for Silverlight configured.
To fix this problem, right-click on your application, and click the MIME types button in the HTTP-headers tab.
Add the following:
.xaml    application/xaml+xml
.xap    application/x-silverlight-app
.xbap    application/x-ms-xbap

Now, the web server should be able to serve all the required files for the Silverlight client to run.

I was merrily chugging along developing my app with RIA Services, and I started getting AG_E_PARSER_BAD_PROPERTY_VALUE errors. The error seemed like a XAML error and stated Bad Property Error and the line and column where it occurred. But it was the line where the DomainContext was specified, like so:

<riaControls:DomainDataSource.DomainContext>
    <App:fooDomainContext/>
</riaControls:DomainDataSource.DomainContext>

The error was being raised when the Domain Data Source was initializing, but it was cryptic and just had the line and column number.
To get better error details, move the Declarative XAML (above) to the code-behind, so it is like:

fooDomainContext dc = new fooDomainContext();
this.dds.DomainContext = dc;

Now, if there was a problem with Domain Context, you will get a more meaningful error message.

Another tip: If you get an error because an unknown method was being called on the Domain Context, remember the proxy code is auto-generated and the name of the query methods are created by convention, and so a method in your web service Domain Context getFoo() will be named getFooQuery() in the client code. 
If you cannot figure out what the name is, go to the Silverlight client project and click on “Show all Files” This will show hidden auto-generated files and the Domain Context proxy would be under the hidden folder “Generated Code” – foo.Web.g.cs, where foo.Web is your Web Service project. Open the hidden file, located the Domain Context class and see what methods it provides. Hopefully, you can locate the query method name.

Hope that helps…

One issue I came across while using a ListBox with a DataTemplate, instead of a DataGrid was a compiler/xaml error stating that the Items collection should be empty…

This was a little baffling and I was wondering if there were data-binding problems in the code-behind, etc. Turns out the source of the problem was in the XAML.

I had:

<ListBox x:Name=”foo”>
     <DataTemplate>

and I needed to add in a ItemTemplate container tag, like so:

<ListBox x:Name=”foo”>
     <ListBox.ItemTemplate>
          <DataTemplate>

I think the error was happening because the framework was considering the DataTemplate as an item in the list, and hence the Items collection was not empty and messed with the binding.
Adding the ItemTemplate tag specified it as a template, and not a list-item

After porting an old Silverlight application, I started getting compiler errors stating “The name ‘InitializeComponent’ does not exist in the current context”.

This was baffling and after poking around a bit, this is how I resolved it.
Note: My solution may be specific to my Silverlight installation and computer setup. I never quite know what the various bits are.

Essentially, the build is not generating the hidden partial classes named foo.g.cs for the application or page foo.xaml. These contain the InitializeComponent and are hidden files under the obj/Debug folder.

To fix the problem, right-click on the project with the errors and select “Unload”
Then right-click and select “Edit xxx”. This will load the project file xml in the IDE.

Locate the item <ItemGroup>, which is somewhere towards the bottom.
You will see blue squiggly lines under the Application element and the Page.

Change them to be <ApplicationDefinition Include=xxx> and <Page Include=xxx>
Note: There will be a blue squiggly line under <SilverlightApplication>true</SilverlightApplication>, but no need to fix that

Save the xml and right-click the Project and select Load. Say ok to unload open file and load and compile project.” Hopefully your problem was resolved.