Twitter integration in Windows 8

Integrating Twitter in your own Windows 8 store app requires a lot of knowledge of their API, this because of the strict requirement of authentication through OAuth ! We needed this twitter integration, because we want to show the twitter avatars of several users in our app!

If you are up to it, you can implement your own REST code… But if you are a bit like me and would think others are far more educated to get it right, you can go for a library that does all the hard work for you!

I choose to use Linq2Twitter ( made by @JoeMayo ) for our latest Windows 8 store application, because for me, the Linq format just seems the best way to deal with the API.

Now let me show you how you can add that twitter integration in your Windows 8 app!

First things first, when you want to do any twitter API calls from within your own app, you’ll need to register the app through their Developer site, be sure to also fill in a callback URL ! When this is done and your app was verified, you’ll get 2 keys that are needed in your .net code:

  • Consumer key
  • Consumer secret

When you have that in place, you can add the code needed to get your user to allow your app to talk with his twitter account. Within a Windows 8 store app, this  is done with the WinRtAuthorizer from Linq2Twitter. Just take care of saving the passed through user credentials, so you can reuse them the next time your app starts!

 

When this is done, you normally should have an authorized twitter context that you can use inside your app. What I needed in our app was retrieving several twitter user handle images and also needed to be able to tweet a certain text.

For searching a lot of users at the same time you can use the following code:

 

What we do here is perform a twitter lookup with a string concatenated list of twitter screen names. If the search went ok, we get back an Enumerable list of twitter Users that contain a lof of info about the user! In our case we needed the user.ProfileImageUrl

So that’s it, nothing more to it, be sure to check out the codeplex site of Linq2Twitter for much more details on what you can do with this library.

QR Code generation

For some time now, most people here in Belgium are beginning to notice the new bar code thing that is popping up all over the place… I’m talking about QR codes!
They are not difficult to spot, because of there special resemblance. But it’s not the fact that they are more advanced in resemblance that makes them useful 🙂 It’s the fact that they can contain data value and data metadata!!

In other words not every QR code represents the same thing… you can have it represent:

  • Text
  • Website URL
  • Telephone Number
  • SMS Message
  • Email Address
  • Email Message
  • Contact Details (VCARD)
  • Event (VCALENDAR)
  • Google Maps Location
  • Social Media ( like a facebook profile )

And accordingly to the content type your mobile phone or computer will perform the needed action to represent the data value correctly!

Now most people don’t know exactly how to use these QR Codes, let alone how to generate them… because of this you see service website like this one popping up all over the place.
But the funny thing is, everybody can get these QR Codes for free from a very nice online service! It’s called Google Chart API.
We are currently using it in a small ASP.Net MVC application at our company and we are using it to get Google maps location data represented through a QR Code.

The only thing you need to do is read the instructions up on the Google Chart website here!
You just have to ‘create’ the correct http request url and when ‘executed’ you’ll get back an image.
If you use it correctly you can for instance represent longitude-latitude coördinates on Goolge maps by using following code:

http://chart.apis.google.com/chart?cht=qr&chs=85x85&chl=http://maps.google.com/m?q=51.10231200,4.53204000

This will give you following QR Code, just try to scan it with your iPhone or Android smartphone 🙂

SelectionChangeCommited event on Combobox not triggered

There seems to be a problem with the .Net Combobox control ! Microsoft is stating it isn’t actually a real problem, but still it is something a programmer should know and if needed take care of.

Let me first explain the problem… If you use the mouse to open a combobox, but then use the arrow keys to navigate in the list and change focus with the tab key, the combobox will select the highlighted value, but will NOT trigger the SelectionChangeCommited event !!
So in other words, even though the programmes subscribes to this event to know when a user has selected a value, the program will not notice it.

According to this post : http://weblogs.asp.net/grobinson/archive/2005/09/21/425711.aspx, Microsoft is aware of this and regards it as standard behaviour. So we are left with the only option to write a workaround ourselves.

I’ve found a good starting solution here : https://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3409311&SiteID=1, but I think this can be improved ! Because with this given solution, if a user still would use the mouse to select an item we would get the SelectionChangeCommited event twice.
So the workaround is to suppress the base event and only trigger our own. Here is the code…

[tags]SelectionChangeCommitted, combobox, c#[/tags]

Serialize/Deserialize objects in .Net – don’t forget to initialize

Recently in our project I had a weird exception popping up in C#. We found the problem and fixed it… but the problem was something we didn’t expect ! So I’ll post my findings here so other people can benefit from it.
I must say, I don’t know if this behaviour is anywhere described in MSDN, so could be I missed the information.

Consider this class Foo with only 1 member O1 that is Serializable.

[Serializable]
public class Foo
{
private object _O1;

public object O1
{
get { return _O1; }
set { _O1 = value; }
}

public Foo()
{
O1 = new object();
}
}

Now we are going to save an instance of this Foo class to file through Serialization ! ( here done through unittesting with NUnit )

[Test]
public void SerializeTest()
{
Foo foo = new Foo();
Assert.IsNotNull(foo.O1);

BinaryFormatter formatter = new BinaryFormatter();
formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
FileStream stream = new FileStream(@"c:\temp\foo.dat", FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, foo);
}

Now we have a binary representation of Foo on file, called foo.dat !
After this, we change the Foo class and add a second member called O2.

[Serializable]
public class Foo
{
private object _O2;

public object O2
{
get { return _O2; }
set { _O2 = value; }
}

private object _O1;

public object O1
{
get { return _O1; }
set { _O1 = value; }
}

public Foo()
{
O1 = new object();
O2 = new object();
}
}

And now for the big problem… we Deserialize the foo.dat file with this new Foo class code.

[Test]
public void DeserializeTest()
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(@"c:\temp\foo.dat", FileMode.Open, FileAccess.Read, FileShare.Read);
Foo foo = (Foo)formatter.Deserialize(stream);
Assert.IsNotNull(foo.O2);
}

And what do you notice ? The test wil FAIL !! Yes… what we didn’t expect was the fact that .Net somehow can initialize the Foo object without going through it’s default constructor !!
Resulting in a Foo object with a O2 member that equals NULL instead of new Object() !

This would mean that if you need to deserialize newer class versions of existing binary objects, you should use the interface ISerializable and use protected Foo(SerializationInfo info, StreamingContext context) to be sure all members ( and new members ) are serialized and initiated ?

Can anyone shed some light on this ?

[Update] Jelle has given me the correct link to the MSDN documentation that gives the answer to this phenomenon !! Thanks Jelle ! http://msdn2.microsoft.com/en-us/library/4abbf6k0(VS.71).aspx

[tags]C#, .Net, Visual Studio[/tags]