Category Archives: C#

Azure Mobile Services and DateTimeOffset

I use DateTimeOffset very extensively, my corresponding SQL Azure table in mobile services uses datetimeoffset(3) column. The issue that I had was somewhere during JSON.NET serialization, the DateTimeOffset lost the offset and got converted to UTC. I couldn’t find any solution, until I experimentally started playing with the Mobile Services Client SerializationSettings, exposed in the latest SDK. Changing DateTimeZoneHandling  didn’t seem to help much until I discovered this post by Carlos. I changed it a little bit, keeping the idea, and voila: my DateTimeOffset now saves in Azure Mobile Services, no problem. If you know a better way, please let me know:

 

                MobileService.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.RoundtripKind;
                // remove date time converter
                var conv = MobileService.SerializerSettings.Converters.Where(c => c is MobileServiceIsoDateTimeConverter).FirstOrDefault();
                if (conv != null)
                {
                    MobileService.SerializerSettings.Converters.Remove(conv);
                }
 
 

Retrieving more data from Azure Mobile Services using paging and LoadAllAsync extension

Azure Mobile Services allow you to take 50 records at a time by default, or 1000 records at maximum. What if you have more records and want to retrieve the entire table? I just created this beautiful extension method to help you get any amount of data you want, given any page size you want. This simple call to LoadAllAsync will asynchronously load ALL data from a WAMS table in pages of 1000 (or whatever number you specify) records (may also be good for bandwidth reasons):

var updatedReports = await azureTable.Where(r => r.complete == true).LoadAllAsync();

And this is an extension method, which elegantly does exactly what it says: loads all data. Enjoy:

        public async static Task<List<T>> LoadAllAsync<T>(this MobileServiceTableQuery<T> table, int bufferSize = 1000)
        {
            var query = table.IncludeTotalCount();
            var results = await query.ToEnumerableAsync();
            long count = ((ITotalCountProvider)results).TotalCount;
            if (results != null && count > 0)
            {
                var updates = new List<T>();
                while (updates.Count < count)
                {

                    var next = await query.Skip(updates.Count).Take(bufferSize).ToListAsync();
                    updates.AddRange(next);
                }
                return updates;
            }

            return null;
        }

Ready to make some money, Windows 8 is coming!


This weekend I invite you to my session at Silicon Valley Code Camp 2012:

We’ll discuss Windows 8 Store, monetization and… a bit of a surprise: for the first time I’ll be announcing my book Professional Windows 8 Programming: Application Development with C# and XAML
, written with a great team of authors: Nick Lecrenski, Doug Holland, Allen Sanders, Kevin Ashley. At this session we’ll focus on practical aspects of monetizing Windows 8 apps. I’ll share some code examples, including: in-app purchase, trial unlocking, placing ads in the apps and using trial model to support monetization. We’ll also touch on more advanced topics, and as an author of several apps in Windows Store, I’ll be happy to answer your questions.

Windows 7 sold over 500 million copies in just three years, if Windows 8 does that well this is going to be one of the biggest opportunities our industry has ever seen. Come learn how to get your app into the Windows Store and how to make money. After all, you want to be the first app in the marketplace and have the early mover advantage, don’t you?

SESSION CODE EXAMPLES INCLUDE: Trial, in-app purchases, store simulation.

1:45 on Saturday

Foothill College
12345 El Monte Road
Los Altos Hills, CA 94022

http://binged.it/WmQYKH

Async CTP – Task based asynchronous programming for Windows Phone

6 code samples for this post

[my apps for Windows Phone that use Async CTP Smile]

Intro

Asynchronous programming is super-interesting especially today, with responsiveness required by all modern devices. In the context of Windows Phone 7, Async CTP Version 3 was released last year, and it’s compatible with SDK 7.1, Silverlight 5 and Roslyn CTP. Our takeaways today is using async in the context of Windows Phone 7, understanding TAP (task based asynchronous programming). I’ll show you how deep the rabbit hole goes: we’ll go beyond basics to exceptions, cancellations, progress, switching threads, combinators and even re-entrancy.

Historical Background

And let me just say: it’s worth learning NOW, because this model is quickly becoming mainstream, especially with Windows 8 not that far on the horizon. In terms of the where we are with Async CTP Version 3: you don’t need to wait for C# 5.0 to start using it. It has been a long time since 1960s when Steve Russel was one of the first to add continuations to LISP on IBM 704. I like this definition of continuations:

Say you’re in the kitchen in front of the refrigerator, thinking about a sandwich. You take a continuation right there and stick it in your pocket. Then you get some turkey and bread out of the refrigerator and make yourself a sandwich, which is now sitting on the counter. You invoke the continuation in your pocket, and you find yourself standing in front of the refrigerator again, thinking about a sandwich. But fortunately, there’s a sandwich on the counter, and all the materials used to make it are gone. So you eat it. :-)

Scheme formally introduced call/cc operations.

Continue reading

5 Windows Phone games great for holiday travel

In my previous post we discussed using Expression Blend to create a flipping card animation. Since the Holiday season just started, I’d like to share these games I built, that use this technique. Happy Holidays, have a great holiday season, and for those skiers: Let It Snow!

Continue reading

Flipping Card animation for Windows Phone 7 using Expression Blend

Download Source

In this post I’ll show you how to create a card flipping animation for Windows Phone 7, using Expression Blend. The source code is available for download in the attachment. First, I create a Windows Phone 7 project from scratch, using Expression Blend, you can also create a project in Visual Studio, right click on any page and select “Open with Expression Blend…” context menu option. I actually used this technique in real Windows Phone games I built:

ApplicationIconUno
DUO
*****
ApplicationIconCrazyEights
Crazy
Eights
ApplicationIcon101
101 Card
Game
ApplicationIconMauMau
Mau
Mau
ApplicationIconSwitch
Switch
*****

Continue reading