Thursday, December 23, 2010

Adding an item to a list and scrolling it into view #wp7dev

For reasons can't explain rightly explain, it was a bit tricky to add an item to a list (in a Windows Phone 7 application - unsurprisingly) and then scroll the list so the new item was displayed.

I eventually got this working by forcing the call to scroll the new item into view on the UI thread.

Consider this in a default DataBound application:

  private void ApplicationBarIconButton_Click(object sender, EventArgs e)
  {
    App.ViewModel.Items.Add(new ItemViewModel
                                {
                                  LineOne = "new L1",
                                  LineTwo = "new L2",
                                  LineThree = "new L3"
                                });
 
    Dispatcher.BeginInvoke(() =>
      MainListBox.ScrollIntoView(MainListBox.Items.Last()));
  }


Without doing this, the list will scroll to the item which was previously the last on the list.

Tuesday, December 21, 2010

Rolling your own cut and paste is acceptable in the marketplace #wp7dev

I'm sure you'll be thrilled to know that my Scratch Pad app is now available in the Marketplace. More important than creating a custom cut and paste implementation, this proves that it's possible to store data on the device and share it between applications.

In time, I'll explain how I did this. And, also, why you, probably shouldn't!

Until then please feel free to tweet about my app and be sure to use the hashcode #wp7comp or post your comments here.

Friday, December 10, 2010

Cut and Paste with the current #WP7Dev tools

For a lot of people the lack of native Cut & Paste support in the current (initial) version of Windows Phone 7 is a big deal.

I've heard a surprisingly large amount of people tell me that it's essential that they can copy and paste text* on a phone. Personally I hardly ever used this feature when I was using a phone which had it though.

I've pointed out to a few people that it's possible to implement the cutting, copying and pasting of text within an application quite easily. (It actually only take a handful of lines of code.) But no-one (AFAIK) has taken the initiative to do this. Before now...

The issue isn't really being able to cut and paste text though. What people really want is a way to easily share data between applications. When people say "cut and paste" they, typcially, mean they want to take (copy) some data (text) from one application and then use (paste) it in another application.

As native functionality, this isn't currently possible.
However this will be coming in an remote update early next year.

So if we want copy and paste in our apps we have to either wait or try and do it ourselves.

Surprisingly, the sandboxed nature of 3rd party applications on the phone has lead many (most?) to assume that this isn't possible.

I beg to differ!

Take a lok at this video of 3 apps I wrote. 2 accept the entry of text and support methods of multiple word selection (also not currently available natively) and cutting, copying and pasting text. The third app just shows what is in the "Shared Clipboard". Yes. These apps all have access to the same clipboard. And, no, this isn't done by sending the data to a remote web server or anything like that. It's also not using anything not publically available and only uses public, authorized API methods.



To try and prove that this is really possible I've submitted Scratch Pad to the marketplace. When it's accepted you can try it for yourself. Plus acceptance will show it's not using anything it shouldn't (or you couldn't) and that it doesn't require a data connection.

With native support for cut and paste coming in early 2011 (most people are assuming in January) I'm not planning on sharing the clipboard code as there is a small side effect to using it that it's probably best not to burden users with. But, I assume that you're smart enough to work out how I did this if you really wanted to. ;)

* It's always text that people want to copy and paste. I haven't, yet, heard anyone give a case for wanting this for any other data type on a phone.

Tuesday, December 07, 2010

[WP7Dev] Gotchas when detecting network connectivity

When developing for an occassionaly connected device it's useful to know if there's a connection currently available.

The Windows Phone 7 SDK makes this very easy with the GetIsNetworkAvailable() method on the NetworkInterface class.

There are, however, two gotchas you need to be aware of when debugging.

1. If you call this method in code running in the emulator it will always return true.

2. If you call this method in code running on a tethered device (as you may have connected when debugging on a real device) it will always return true.

In both instances you'll need to code around the issue. (Or set break points and move the next/current executing line.)

You should also write code to handle the situation where a network connection is available but your specific web service/site isn't.

Hope this saves someone some head scratching.