Iâ€™ve been launching headlong into Windows Phone 7 development as can be seen from my previous post and Iâ€™ve come across some little tit bits I thought Iâ€™d post up here.
Jump Start Training
I can’t begin to say how helpful the jump start training courses have been.
So far I’ve done the introduction, building silverlight 1 & 2, and advanced applications 1 & 2. Some of the stuff I already knew from other blog posts (like the application lifecycle stuff) but Iâ€™ve learnt quite a few new things.
Windows Phone Developer Blog: Windows Phone 7 Jump Start Training
Getting Focus and Active Control
In old school WinForms there is a property of a form called ActiveControl which returns the control which is currently active. This property doesnâ€™t exist in Silverlight. Instead you need to use the FocusManager static class and its GetFocusedElement static method.
object obj = FocusManager.GetFocusedElement();
if (obj != null && obj is TextBox)
TextBox txt = obj as TextBox;
txt.Text = "Has Focus";
Beta Tools Warning
In the current beta tools Control.Focus() does not correctly transfer focus to the control.
DataBinding and the ApplicationBar
I really like the way the application bar works to make a Windows Phone 7 application look, well, like a Windows Phone 7 application.
However as has been noted elsewhere the ApplicationBar is not a Silverlight control in the normal sense. This means that you cannot use data binding when setting it up, you canâ€™t use Commandâ€™s to hook up events and various other little tit bits.
Here are two links to MSDN documentation about the ApplicationBar.
How to: Add an Application Bar to Your Application for Windows Phone
Application Bar Best Practices for Windows Phone
The fact the application bar does not appear on the Silverlight visual tree results in two other issues. Firstly, setting a x:Name property on your buttons and menu items does not work. The fields in your code will always be null. You have to use the ApplicationBar.Buttons and ApplicationBar.MenuItems arrays to get access to the buttons and menu items.
Secondly, if you set up your controls with data binding on the form then click on an application bar button the data binding for the control with the focus is not updated. These two issue are discussed on Laurent Bugnionâ€™s blog here: Two small issues with Windows Phone 7 ApplicationBar buttons (and workaround).
Using the above code for determining the current focus control I came up with an elegant solution when you have more than one text box like so:
object focusObj = FocusManager.GetFocusedElement();
if (focusObj != null && focusObj is TextBox)
var binding = (focusObj as TextBox).GetBindingExpression(TextBox.TextProperty);
So far I have only tested this with the TextBox.Text property. CheckBoxâ€™s donâ€™t seem to have this issue. Other controls like list boxes might also need to be taken into account.
It should also be noted that in the final release an alternative solution would be to simply move the focus to another control, or to the page itself (IsTabStop must be set to true for the Focus() method to work on the page) to get the data binding to update.
Flickr (and OAuth) authentication
Finally a word on Flickr (and by extension OAuth) authentication.
The usual process for desktop app authentication for Flickr is that you request a temporary value from Flickr (called the â€˜frobâ€™), then redirect to the Flickr web site where the authentication is processed. Then when you return to the application you use that frob to get the authentication token.
At first I tried to use the WebBrowserTask to kick off the authentication process in much this manner. This works fine except for one thing. The only way to get back to your application is to repeatedly hit the back button to come all the way through the IE history to exit IE and return to your app. The authentication process is at possibly 4 pages (Yahoo login, Authentication warning, Authentication approval and Authentication Complete pages) and there is nothing to stop the user continuing to use the web browser after they have authenticated to continue browsing the internet, so this didnâ€™t seem like a good way.
Then I tried using the WebBrowser control. This works, but I couldnâ€™t find any way to tell if the user had finished authenticating.
I was OK with this, but wanted to improve the experience. I finally came across this post discussing OAuth (which follows the same kind of flow as Flickr) for Windows Phone 7: http://blog.markarteaga.com/OAuthWithSilverlightForWindowsPhone7.aspx.
I think this gives a much cleaner UI and I shall be implementing it in the near future.
Other useful Links
Me on Twitter: http://twitter.com/samjudson
The # wp7dev tag on Twitter: http://twitter.com/#search?q=%23wp7dev
Daily â€˜twitterâ€™ newspaper: http://paper.li/tag/wp7dev
Windows Phone 7 Developer Blog: http://windowsteamblog.com/Windows_Phone/b/wpdev/
Windows Phone 7 Developer site: http://developer.windowsphone.com/
Update: Tombstoned Apps may not be reactivated
I forgot to mention – if the user starts a new version of your application then any tombstoned version will be removed from the back stack. e.g. If they go Start Button â€“> Your App â€“> Start Button â€“> Your App then pressing the back button will exite your application and take them to the Start screen, and pressing it again will do nothing â€“ no second instance of your app will be reactivated.
This does mean however that if your app is loading, you can clear down any existing data that may be hanging around from tombstoned apps.